Table of Contents
Using awk
Command
Use the awk
command to print every nth line from file in Bash.
1 2 3 4 5 6 7 8 9 10 11 12 |
This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line This is 6th line This is 7th line This is 8th line This is 9th line This is 10th line |
1 2 3 4 5 6 |
#!/bin/bash file="test.txt" read -p "Enter the number n for the nth line you want to print: " n awk 'NR % '$n' == 0' "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 5 |
Enter the number n for the nth line you want to print: 4 This is 4th line This is 8th line |
In this example, the awk
command gets every 4th line from the file test.txt
. Here, the file variable contains the file name from where you want to retrieve the lines. Then, the user is prompted using the read
with the message Enter the number n for the nth line you want to print:
.
Let’s understand the 'NR % '$n' == 0'
expression:
NR
is a built-in variable in theawk
command representing the line number.%
is the modulus operator.n
is the input taken from the user.
Here, the 'NR % '$n' == 0'
expression checks if a line number NR
is divisible by n
. If the condition is true (line number is divisible by n
), the default action of awk
will be executed to print the line.
In the given scenario, the objective was to display every 4th line from a file. Specifically, the lines divisible by four are printed on the console.
If you want to put every 4th line into new file, you can use >
operator.
1 2 3 4 5 6 |
#!/bin/bash file="test.txt" read -p "Enter the number n for the nth line you want to print: " n awk 'NR % '$n' == 0' "$file" > output.txt |
1 2 3 |
Enter the number n for the nth line you want to print: 3 |
1 2 3 4 5 |
This is 3rd line This is 6th line This is 9th line |
Using sed
Command
Use the sed
command to print every nth line from file in Bash.
1 2 3 4 5 |
#!/bin/bash file="test.txt" sed -n '0~2p' "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 5 |
This is 2nd line This is 4th line This is 6th line |
In this example, the sed
command utilized the first~step
address form, where the first
represents the line number to start from and the step
represents the increment to select every nth line. Additionally, the -n
flag is used to suppress the automatic printing of lines. In other words, the first~step
form means to get every nth line starting from the line first.
So, here the '0~2p'
pattern is used to select every 2nd line, starting from the first line(Note: zero is nothing; it means the first line).
The
-n
option tellssed
to suppress the automatic printing of lines. By default,sed
prints every line, but with-n
, we control the printing explicitly.
Now, if you want to get the range of lines from the specified file, check the below example:
1 2 3 4 5 6 7 |
#!/bin/bash file="test.txt" read -p "Enter the starting line number: " start read -p "Enter the ending line number: " end sed -n "${start},${end}p" "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 5 6 7 |
Enter the starting line number: 3 Enter the ending line number: 5 This is 3rd line This is 4th line This is 5th line |
In this bash script, the user is prompted using the read
command to enter the starting line number, which is stored in the start
variable and the ending line number, which is stored in the end
variable, to get the range of lines from the specified file which is test.txt
in the above case. Then, the sed
command is used with the pattern "${start},${end}p"
to print lines from the starting line number to the ending line number specified by the user.
On execution of the above script, the user will ask to enter the starting and ending line numbers. After that, the sed
command will display the range of lines specified by the user, from line 3
to 5
in the above case.
Look at another example below for a better understanding:
1 2 3 4 5 |
#!/bin/bash file="test.txt" sed -n '1p;2~4p' "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 5 6 |
This is 1st line This is 2nd line This is 6th line This is 10th line |
The sed
command used the '1p;2~4p'
pattern in this example. Let’s breakdown the given pattern below:
1p
: It is used to print the first line.2~4
: It is used to print every 4th line starting from line 2.
If you want to get just 4th line, you can use below code:
1 2 3 4 5 6 |
#!/bin/bash file="test.txt" read -p "Enter the line number you want to print: " n sed -n "${n}p" "$file" |
1 2 3 4 |
chmod +x BashScript.sh ./BashScript.sh |
1 2 3 4 |
Enter the line number you want to print: 4 This is 4th line |
In this example, the sed
command gets the 4th line from a file test.txt
. Here, a user is asked to enter the line number he wanted to print, which is stored in the n
variable using the read
command with the -p
option for the prompt. Then, the sed
is used with the -n
option to suppress the automatic printing.
After that, the pattern ${n}p
is used to print the number of lines n
specified by the user from the given file. Again, remember to replace test.txt
with the actual path of your file.
Note that the chmod +x BashScript.sh
command grants execute permissions to the file BashScript.sh
, enabling us to run the script directly from the command line using ./BashScript.sh
(assuming you are in the same directory as the script).
There is another way to print every nth line using sed command.
1 2 3 4 5 |
#!/bin/bash file="test.txt" sed -n 'n;n;n;p;' "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 |
This is 4th line This is 8th line |
In this bash script, the sed
command is used with the -n
option and the pattern 'n;n;n;p'
to retrieve every 4th line from a specified file. Here’s what it does:
n
: Tells thesed
command to read the next line from the input and replace(or skip) the current line in the pattern space. In this case,n
is used three times consecutively, meaning it reads and discards the next three lines in the input.p
: Tells thesed
command to print the current line to the output. It is executed after the three consecutiven
commands, so it prints the fourth line from the input.
On execution of the above script, the sed
command skipped the first three lines and displayed every 4th line from the file.
Using perl
Command
Use the perl
command to print every nth line from file in Bash.
1 2 3 4 5 6 |
#!/bin/bash file="test.txt" read -p "Enter the number n for the nth line you want to print: " n perl -ne 'print if ($. % '$n') == 0' "$file" |
1 2 3 |
./BashScript.sh |
1 2 3 4 5 |
Enter the number n for the nth line you want to print: 5 This is 5th line This is 10th line |
In this example, perl
gets every 5th line from a file test.txt
. Here, the -ne
option is used to specify that the Perl code should be applied to each line of the file.
Now let’s understand the expression:
'print if ($. % '$n') == 0'
: This perl code determines which lines to print.$.
: This is a special variable in perl that represents the current line number.%
is the modulus operator.n
is the input taken from the user.
So, the ($. % '$n') == 0'
expression is used to check if the current line is divisible by n
. If the condition is true, print that line. For example, in the above case, the user entered 5 to print every 5th line from a file. So, all the line numbers that are divisible by five are displayed.
That’s all about how to print every nth line from File in Bash.