Print Every nth Line from File in Bash

Bash get every nth line from File

Using awk Command

Use the awk command to print every nth line from file in Bash.

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 the awk 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.

Using sed Command

Use the sed command to print every nth line from file in Bash.

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 tells sed 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:

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:

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:

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.

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 the sed 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 the sed command to print the current line to the output. It is executed after the three consecutive n 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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *