Linux – Running CAT in SCREEN no output to file

awkcatgnu-screenlinux

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.

Best Answer

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).