I have written a bash script that performs 100 iterations of a programme. The output for each of these iterations has 14 lines, but I only need the 14th one for the rest of my analyses (AKA the last line).

I've read the answers to similar questions, but they all assume named lines for the output; whereas the 10th line of my output looks like this:

1 0 0.005 63.01

With no name for grep to hold on to.

I would like to take this 10th line, and for each iteration done in the bash script, attach the output to the bottom of another file (total_output.txt) like so:

left right p-value ratio

1 0 0.005 63.01
1 0 0.02 67.23

And so on for each iteration. Preferably, this would be in the same bash script as well and without having to save out the programme output for each iteration.

I'm still pretty new to Linux and bash scripting, so apologies if this question seems stupid. I couldn't really understand how to apply the answers to similar questions to my problem.


2 Answers 2


You can use tail to only get the last line, and add that line to a file total_output.txt like this: (this only works you the output is always 14 lines and you want the 14th, or if you always want the last line)

your_program | tail -n 1 >> total_output.txt

The command has 3 parts:

  • your_program runs the program and outputs the content of your iteration
  • | tail -n 1 fetches only the last line of this output (prefixed with a pipe)
  • >> total_output.txt adds this last line to the file specified (prefixed with the appending output redirector)

According to your edit, you always want the last line of your output, and not (as originally presented) a specific line number.

While this doesn't invalidate the original answer in your case of a fixed-length output, you can use awk even if the number of lines is variable and you want to only select the last line:

your_program | awk 'END{print}' >> total_output.txt

This makes use of the fact that at end-of-file (or end-of-input), the "current" line is still the last line read.

Original answer

Instead of grep, you can use awk for that purpose:

your_program | awk 'NR==14' >> total_output.txt

This will pipe the output of your_program only to awk, which will print only the 14th line. The output is redirected (in appending mode) to your desired file.

If the output has more than 14 lines, and you want to trim for efficiency, modify to

your_program | awk 'NR==14{print;exit}' >> total_output.txt

to end the awk process immediately after reaching that line.

Note that (since your write the output "has around 10 lines") if your output actually has less lines, nothing will be appended to your file!

You must log in to answer this question.

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