Why does my tail command -n NUM not work

0 votes

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 to the head 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, lines 454 to 459).

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

enter image description here

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?

Apr 13, 2022 in Linux Administration by Soham
• 9,730 points
• 2,005 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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.

answered Apr 14, 2022 by Aditya
• 7,680 points

edited Mar 5, 2025

Related Questions In Linux Administration

0 votes
1 answer

Installation fails on xenial "Repository does not have a release file"

This bug is missing log files that ...READ MORE

answered May 23, 2019 in Linux Administration by Ishaan
• 1,909 views
0 votes
1 answer

“cd” doesn't work in my shell script

One way to get around this is ...READ MORE

answered May 24, 2019 in Linux Administration by Shubham
• 13,490 points
• 3,020 views
0 votes
1 answer

group apache does not exist - using root.

Hi@akhtar, When we install apache wevserver in our ...READ MORE

answered Apr 14, 2020 in Linux Administration by MD
• 95,460 points
• 5,924 views
0 votes
1 answer

Error lscpu: command not found.

Hi@akhtar, By default, this command should present in ...READ MORE

answered Oct 12, 2020 in Linux Administration by MD
• 95,460 points
• 3,281 views
0 votes
1 answer

Amazon Linux: apt-get: command not found

Amazon Linux is CentOs-based which means that ...READ MORE

answered Feb 11, 2022 in Linux Administration by Soham
• 9,730 points
• 4,829 views
0 votes
0 answers

What killed my process and why?

This happened two times. I asked if ...READ MORE

Apr 13, 2022 in Linux Administration by Aditya
• 7,680 points
• 1,657 views
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
• 3,087 views
0 votes
2 answers

Install postgreSQL on Ubuntu

Follow the below commands to install PostgreSQL (PSQL) ...READ MORE

answered Nov 12, 2020 in Database by Prachi
• 140 points
• 3,350 views
0 votes
1 answer

Access progrs prompt without switching accounts

Use the following to get the postgres prompt ...READ MORE

answered Mar 22, 2019 in Database by Mahi
• 2,476 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP