Search code examples
regexbashgrepcattail

Cat some specific line which count from bottom of text file


e.g.

file1:

abc
def
ghi
Hi!
xyz

file2:

111
13215
532
431
2344
Hi!
12fd

Expected outputs:

$ some_command file1
Hi!
$ some_command file2
Hi!

If I want to get Hi!, the pattern that I already knew is they are located at the second line of every files (count from bottom). So, the question is how can I get Hi! with only information that I have which is "2".


Solution

  • Use :

    tail -n 2 files | head -n 1
    

    Explanation :

    tail -n 2 will output the last 2 lines of the input,

    And | head -n 1 will output the first line of the output from tail.