-2

I want to understand the following command in Linux:

# tail -n+454 /path/to/a/file | head -n 6

I expect that tail -n+454 /path/to/a/file prints the lines, starting at line 454 and the following 5 lines.

The | sends that output tohead as an input. Then only the first 10 lines are taken.

Finally, -n 6 defines that only the first 6 lines are printed to the screen.

Did I translate the command correctly?

Now I have the following problem: Let's assume I have a file and the following line in it:

# Step #6: Configure output plugins

I want to print the 5 lines immediately before that line (including that line).

First I checked, what line number my line in question has:

nl /path/to/a/file | grep output

enter image description here

The line number is 459.

I want the 5 lines preceding line 459 as well as line 459 itself (that is, line 454 to 459).

The command tail -n+454 /path/to/a/file | head -n 6 gives me the following output:

enter image description here

...and this is line 380 to 384:

enter image description here

I expected to get lines 454 to 459. What did I not understand? Is my command not correct?

aurumpurum
  • 932
  • 3
  • 11
  • 27

2 Answers2

0

use this command :

head -n $(grep -n "Step #6: Configure output plugins" /path/to/a/file | awk -F ':' '{print $1}') /path/to/a/file | tail -n 5 

note:
that command has two part

a) found line number:

grep -n "Step #6: Configure output plugins" /path/to/a/file | awk -F ':' '{print $1}'

b) filter lines you want :

head -n [LINE NUMBER] /path/to/a/file | tail -n 5 
mah454
  • 1,571
  • 15
  • 38
  • Thanks for your comment. Do you have an idea, why my command (as stated above) does not work as expected? I would like to solve the problem with the tail -n+NUM command. I am looking for an explanation, why it does not work and what I have to change to make it work. – aurumpurum Jul 01 '21 at 21:15
  • actually , you want 5 line above of target . so it's so bad idea to use tail | head . you need to calculate : tail -n $((NUM + 5)) example: tail -n $((459 + 5)) | hean -n 5 – mah454 Jul 01 '21 at 21:33
  • Hey, there is an easier solution to my question. Check my post. – aurumpurum Jul 03 '21 at 18:42
-1

The mistake I made was that I displayed only the non-empty lines in the file, which was wrong.

It's better to use...

nl -ba [FILE]

to number all lines in the file. Then look up the lines of interest and use the head and tail commands (with piping) to get the final results.

Example:

tail -n +539 [FILE] | tail -n 6
tail -n +539 [FILE] | head -n -212
head -n 544 [FILE] | tail -n 6
head -n 544 [FILE] | tail -n +539

All commands lead to the same result.

Another mistake I made was the syntax. There should be a space between -n and +NUM.

By the way, the line numbers in my OP are wrong, because I used the wrong numbering line command. The line I refer to is 544 not 459.

aurumpurum
  • 932
  • 3
  • 11
  • 27
  • 1
    [Counting lines or enumerating line numbers so I can loop over them - why is this an anti-pattern?](https://stackoverflow.com/questions/65538947/counting-lines-or-enumerating-line-numbers-so-i-can-loop-over-them-why-is-this) – tripleee Jul 03 '21 at 20:51