LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash multiple file operation (https://www.linuxquestions.org/questions/programming-9/bash-multiple-file-operation-4175428446/)

porphyry5 09-21-2012 04:20 PM

bash multiple file operation
 
For each file in a directory, I wish to delete the last 57 lines and concatenate each result into a single output file. I have tried both
Code:

for fl in *.html; do head -n "$(($(wc -l < "$fl") - 57))" >> faq.html; done
for fl in *.html; do a=$(($(wc -l < "$fl") - 57)); head -n $a >> faq.html; done

with the same result in each case, the command hangs after producing an empty faq.html.

There is no complaint from the system, and the arithmetic expression produces the correct number of lines to copy from each file. Could someone kindly explain why this is failing?

pixellany 09-21-2012 05:38 PM

You are not giving the head command any input. In the head syntax, you either need a filename--or it will read from standard input. In your case it tries to read from standard input---and nothing is there, so it hangs.

Also, note that the head command can return the LAST N lines of a file---so you don't need that arithmetic.

porphyry5 09-21-2012 05:55 PM

Quote:

Originally Posted by pixellany (Post 4786193)
You are not giving the head command any input. In the head syntax, you either need a filename--or it will read from standard input. In your case it tries to read from standard input---and nothing is there, so it hangs.

Also, note that the head command can return the LAST N lines of a file---so you don't need that arithmetic.

Many thanks.


All times are GMT -5. The time now is 08:17 AM.