1

I am trying to code this command to run in screen along with 2 others that will be using cat to read a file then pipe it into awk for filtering. Here is the command;

screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt | awk '{print $1}' > /var/www/html/filter/first.txt

If I check the screen that is detached I see it filtering the list, when it completes, the file first.txt is created but empty. I saw something about using -L in screen for an output log, but I want the output going to the same folder the non filtered list is in and called first.txt, I cannot figure out why it will successfully run, but the output file is empty every time. If I run it without screen -d -m the file created is not empty. I think I am missing something with the screen command. I just started using screen. I am not sure when else to post.

1
  • per TOP it looks like cat is running, screen is running, but it is not getting piped to AWK
    – LinHost
    May 14, 2015 at 21:14

1 Answer 1

3

The reason this won't work is that the shell splits the command into two parts like this:

screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt

awk '{print $1}' >/var/www/html/filter/first.txt

The screen runs cat on a separate pty. There is then no output on stdout to pipe to awk.

If you want the pipe to be run under screen, you need to group it with another instance of a shell:

screen -d -m bash -c 'cat /var/www/html/filter/unfiltered.txt | awk "{print $1}" >/var/www/html/filter/first.txt'

Having done that, it should become clear that this is a needless use of cat and actually the command can be simplified like this:

screen -d -m bash -c 'awk "{print $1}" </var/www/html/filter/unfiltered.txt >/var/www/html/filter/first.txt'

Notice I have changed the inside quotes from singles to doubles. This is because I have used single quotes to protect the entire command (and particularly $1 from early evaluation).

7
  • awesome! I will give that a try, I saw the bash -c command listed while reading, but being new to screen I could not get the syntax correct, will try this ASAP, thanks so much! Thanks also for the cleanup, learning to bash script and loving it so far, so much to learn still.
    – LinHost
    May 14, 2015 at 22:15
  • Would the above issue also happen if I put the command into a .sh file separating the two filter commands by && such as; cat /var/www/html/filter/unfiltered.txt | awk '{print $1}' > /var/www/html/filter/first.txt && cat /var/www/html/filter/first.txt | awk '{print $1}' > /var/www/html/filter/last.txt I have 2 commands for filtering to run in total
    – LinHost
    May 14, 2015 at 22:18
  • @LinHost you really don't need the cat. But if you were to put your two awk commands in a script file you could call that directly from screen, yes. May 15, 2015 at 0:47
  • I will give it a try without cat, i see your point about cat, I just copied the command from above as an example. If it works I will mark your answer.
    – LinHost
    May 15, 2015 at 16:58
  • I would rather not use a sh file if I can do both commands, first one then second when the first is done in one line. This is in a PHP file so could I still use && between the 2 commands or do I use ; ?
    – LinHost
    May 15, 2015 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .