- How to add line numbers in every line using shell command?
- How to Display Specific Lines of a File in Linux Command Line
- Display specific lines using head and tail commands
- Print a single specific line
- Print specific range of lines
- Use SED to display specific lines
- Use AWK to print specific lines from a file
- Print Line Numbers In AWK
- Print Line Numbers
- Print Line Numbers Starting From 1
- Print Line Numbers with grep
- Print Line Numbers with perl
- How to Use Linux Cat Command
- Basic Usage of Linux Cat Command:
- Printing Line Numbers:
- Numbering Only Non Blank Lines:
- Removing Repeating Empty Lines:
- Printing Tab Characters:
- Printing End of Line Characters:
- Printing Non-Printing, Tabs, and EOL Characters:
- About the author
- Shahriar Shovon
How to add line numbers in every line using shell command?
You may want to tune w idth option according to the total number of lines in the file (if you want numbers to be aligned nicely).
1> PSS-A (Primary A) 2> PSS-B (Primary B) 3> PSS-C (Primary C) 4> PSS-D (Primary D) 5> PSS-E (Primary E) 6> PSS-F (Primary F) 7> PSS-G (Primary G) 8> PSS-H (Primary H) 9> PSS-I (Primary I) 10> SPARE (SPARE)
nl treats lines that contain a sequence of 1, 2 or 3 \: strings specially. Use -d $’\n’ to avoid that. Also, by default, it doesn’t number empty lines. Use -ba to number every line.
@myrdd, $’. ‘ comes from ksh93 and is also supported by zsh , mksh , busybox sh, FreeBSD sh and bash at least. It’s not standard yet, but is planned for inclusion in the next major POSIX version.
@StéphaneChazelas thanks. for reference, there’s a question on $’. ‘ (ANSI-C Quoting) portability: unix.stackexchange.com/questions/371827/…
If you want the same format that you have specified
awk ' " $s>' inputfile > outputfile
otherwise, though not standard, most implementations of the cat command can print line numbers for you (numbers padded to width 6 and followed by TAB in at least the GNU, busybox, Solaris and FreeBSD implementations).
cat -n inputfile > outputfile
Or you can use grep -n (numbers followed by : ) with a regexp like ^ that matches any line:
grep -n '^' inputfile > outputfile
Yes. both the commands are working. but in cat command its printing the line numbers. but not exactly what I wanted. but awk ‘
In Linux/Unix, there are almost always multiple ways to do common tasks. Just for completeness, here are some other ways you can do it besides the obvious:
From an old command to format text to send to a line printer. The ‘-t’ will omit header and footer information that are not relevant to a terminal.
Here’s a cute sed method that prints the line number on every other line. We use ‘paste’ to fold them into a single line:
Or, we can use the one true editor, ed:
Or, ex, vi’s non-cursor-addressing predecessor:
printf 'set number\ng/^/p\n' | ex /etc/passwd
And one final complicated answer, requiring ksh93 or bash (and the seq command. Using the .. range and an eval statement is left as an exercise):
Tested on Debian Linux, FreeBSD, and Solaris 10 (the last fails there because of no ‘seq’).
How to Display Specific Lines of a File in Linux Command Line
Here are several ways to display specific lines of a file in Linux command line.
How do I find the nth line in a file in Linux command line? How do I display line number x to line number y?
In Linux, there are several ways to achieve the same result. Printing specific lines from a file is no exception.
To display the 13th line, you can use a combination of head and tail:
head -13 file_name | tail +13
Or, you can use the sed command:
To display line numbers from 20 to 25, you can combine head and tail commands like this:
head -25 file_name | tail +20
Or, you can use the sed command like this:
A detailed explanation of each command follows next. I’ll also show the use of the awk command for this purpose.
Display specific lines using head and tail commands
This is my favorite way of displaying lines of choice. I find it easier to remember and use.
Print a single specific line
Both head and tails commands are used to display the contents of a file in the terminal.
Use a combination of head and tail command in the following function the line number x:
You can replace x with the line number you want to display. So, let’s say you want to display the 13th line of the file.
[email protected]:~$ head -13 lines.txt | tail +13 This is line number 13
Explanation: You probably already know that the head command gets the lines of a file from the start while the tail command gets the lines from the end.
The “head -x” part of the command will get the first x lines of the files. It will then redirect this output to the tail command. The tail command will display all the lines starting from line number x.
Quite obviously, if you take 13 lines from the top, the lines starting from number 13 to the end will be the 13th line. That’s the logic behind this command.
Print specific range of lines
Now let’s take our combination of head and tail commands to display more than one line.
Say you want to display all the lines from x to y. This includes the xth and yth lines also:
Let’s take a practical example. Suppose you want to print all the the lines from line number 20 to 25:
[email protected]:~$ head -25 lines.txt | tail +20 This is line number 20 This is line number 21 This is line number 22 This is line number 23 This is line number 24 This is line number 25
Use SED to display specific lines
The powerful sed command provides several ways of printing specific lines.
For example, to display the 10th line, you can use sed in the following manner:
The -n suppresses the output while the p command prints specific lines. Read this detailed SED guide to learn and understand it in detail.
To display all the lines from line number x to line number y, use this:
[email protected]:~$ sed -n '3,7p' lines.txt This is line number 3 This is line number 4 This is line number 5 This is line number 6 This is line number 7
Use AWK to print specific lines from a file
The awk command could seem complicated and there is surely a learning curve involved. But like sed, awk is also quite powerful when it comes to editing and manipulating file contents.
[email protected]:~$ awk 'NR==5' lines.txt This is line number 5
NR denotes the ‘current record number’. Please read our detailed AWK command guide for more information.
To display all the lines from x to y, you can use awk command in the following manner:
It follows a syntax that is similar to most programming languages.
I hope this quick article helped you in displaying specific lines of a file in Linux command line. If you know some other trick for this purpose, do share it with the rest of us in the comment section.
Print Line Numbers In AWK
awk is a popular Linux and Unix to work with text files and command output by manipulating them. While using the awk command it can be useful to add line numbers. This can be especially beneficial for the source codes or configuration files. In this tutorial, we examine how to print line numbers in awk in a different way.
Print Line Numbers
The print command can be used for the awk in order to print line numbers. In the following example, we start printing numbers from 0. The i variable is used to store line numbers and ++ operators have used the increase the i variable in every line. In the following example, we print line numbers for the file named phpinfo.php .
Print Line Numbers Starting From 1
Alternatively, we can start the line numbers for the awk command from 1. The variable i is set as 1 and incremented in every line.
We can also use the NR-1 statement in order to print line numbers. The starting number like 0 or 1 is specified with the NR like below.
Print Line Numbers with grep
Alternatively to the awk command the grep command can be used to print lines.
Print Line Numbers with perl
The perl command is another popular alternative to the awk command to print line numbers.
$ perl -ne 'print $.,":",$_ if /bla/' phpinfo.php
How to Use Linux Cat Command
The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.
The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.
In this article, I am going to show you how to use the Linux cat command. So, let’s get started.
Basic Usage of Linux Cat Command:
The basic and most common use of the Linux cat command is to use it without any command option.
For example, to view to contents of the /etc/hosts directory, run the cat command as follows:
As you can see, the contents of the /etc/hosts configuration file are printed on the screen.
Printing Line Numbers:
Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.
To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:
As you can see, the line numbers are displayed.
Numbering Only Non Blank Lines:
If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.
In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.
As you can see, with the -n option, all the lines (including blank lines) are numbered.
With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.
Removing Repeating Empty Lines:
A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.
You can use the -s option of the Linux cat command to remove repeated empty lines as follows:
Printing Tab Characters:
In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.
Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.
To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:
As you can see, the tab characters are displayed as ^I.
Printing End of Line Characters:
If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.
For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:
As you can see, the EOL characters are printed.
Printing Non-Printing, Tabs, and EOL Characters:
Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:
But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.
As you can see, the outputs are the same.
So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.
About the author
Shahriar Shovon
Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.