I am looping through a list of files, extracting the final line, and printing out columns 8, 9, and 10. I need to also print to the output the 'event number', which is essentially the total number of records being processes (NR). How do I print the event/record number in the first column, outputting to the output file, such as what I have below?
for i in `ls -d *mcp`; do
tail -1 "$i" | awk '{ printf "%s %s %s\n", $8, $9, $10}' >> ${Pout}${output}
done
echo "Finished Looping through each file."
What I want as the output is:
1 45 60 5
2 30 67 3
3 40 12 4
.
.
.
where the '45 column represents $8, 60 represents $9, and 5 represents $10. the 1,2,3, etc. is what I need to output. I essentially need to print the line number.
for i in
ls -d *mcp; do tail -1 "$i" | awk '{ printf "%d %s %s %s\n",NR, $8, $9, $10}' >> ${Pout}${output} done
I get the following output:
1 -0.242 125.104 35.0 1 -6.308 151.717 28.1 1 13.764 144.429 130.0 1 -56.022 -27.779 109.3 1 -9.461 156.412 4.0
Instead of all ones in the first column I want 1,2,...n. Does that clear things up?
– geeb.24 May 13 '15 at 19:30ls
.for i in \
ls -d mcp`` isn’t just inefficient; it produces wrong results* if filenames contain certain special characters. (2) Don’t post multi-line commands or output in comments. Clarifications to the question, to include things that you’ve tried, results that you’ve gotten, and results that you want, belong in the question — [edit] the question to put them there. (3) When you do use command substitution, use$(…)
instead of\
…``. (4) If you must display a\`` in code in a comment, type
\``. – G-Man Says 'Reinstate Monica' May 14 '15 at 09:37