Bash tool to get nth line from a file

bash get nth line of output
awk print nth line
how to get specific lines from a file in unix
how to display specific lines from a file in linux
sed print every nth line
bash read file line by line
sed print nth line to end of file
linux get nth line of output

Is there a "canonical" way of doing that? I've been using head -n | tail -1 which does the trick, but I've been wondering if there's a Bash tool that specifically extracts a line (or a range of lines) from a file.

By "canonical" I mean a program whose main function is doing that.


head and pipe with tail will be slow for a huge file. I would suggest sed like this:

sed 'NUMq;d' file

Where NUM is the number of the line you want to print; so, for example, sed '10q;d' file to print the 10th line of file.

Explanation:

NUMq will quit immediately when the line number is NUM.

d will delete the line instead of printing it; this is inhibited on the last line because the q causes the rest of the script to be skipped when quitting.

If you have NUM in a variable, you will want to use double quotes instead of single:

sed "${NUM}q;d" file

3 ways to get the Nth Line of a File in Linux, The need to get/print a particular line of a file on the Linux shell is a common task. Luckily there are various ways to do this. Below are three  2nd word from lines in a text file. Bash tool to get nth line from a file. 758. Read a file line by line assigning the value to a variable. Hot Network Questions


sed -n '2p' < file.txt

will print 2nd line

sed -n '2011p' < file.txt

2011th line

sed -n '10,33p' < file.txt

line 10 up to line 33

sed -n '1p;3p' < file.txt

1st and 3th line

and so on...

For adding lines with sed, you can check this:

sed: insert a line in a certain position

Can I access Nth line number of standard output?, If the file is long and you're only interested in one line, use sed to print the desired line and quit, or tail to remove the previous lines (Note that tail -n +$n skips n-1 lines, i.e. its output starts with the nth line.) If you want to read all lines, you can put them in an array (ksh/bash/zsh only). That's the script outputting file paths: For convenience, if you use bash, just add the below to your ~/.bash_profile. And, the next time you log in (or if you source your.bash_profile after this update), you will have a new nifty "nth" function available to pipe your files through.


Write a bash script to print a particular line from a file, Suppose we have two files, file1.txt and file2.txt, We can use the above commands and print particular line from multiple files by '&'. awk: $  This way you save CPU time See time comparison at the end of the answer. awk 'NR == num_line {print; exit}' file. If you want to give the line number from a bash variable you can use: awk 'NR == n' n=$num file awk -v n=$num 'NR == n' file # equivalent.


With awk it is pretty fast:

awk 'NR == num_line' file

When this is true, the default behaviour of awk is performed: {print $0}.


Alternative versions

If your file happens to be huge, you'd better exit after reading the required line. This way you save CPU time See time comparison at the end of the answer.

awk 'NR == num_line {print; exit}' file

If you want to give the line number from a bash variable you can use:

awk 'NR == n' n=$num file
awk -v n=$num 'NR == n' file   # equivalent

See how much time is saved by using exit, specially if the line happens to be in the first part of the file:

# Let's create a 10M lines file
for ((i=0; i<100000; i++)); do echo "bla bla"; done > 100Klines
for ((i=0; i<100; i++)); do cat 100Klines; done > 10Mlines

$ time awk 'NR == 1234567 {print}' 10Mlines
bla bla

real    0m1.303s
user    0m1.246s
sys 0m0.042s
$ time awk 'NR == 1234567 {print; exit}' 10Mlines
bla bla

real    0m0.198s
user    0m0.178s
sys 0m0.013s

So the difference is 0.198s vs 1.303s, around 6x times faster.

Bash tool to get nth line from a file?, Is there a "canonical" way of doing that? I've been using `head -n | tail -1` which does the trick, but I've been wondering if there's a Bash tool  In Bash, I want to get the Nth word of a string hold by a variable. Looping through the content of a file in Bash. 520. Fail to draw a line in a node


Unix Sed Tutorial: Printing File Lines using Address and Patterns, The name sed is an abbreviation for stream editor, and the utility derives many of its N,$ with “p” command prints from Nth line to end of file. Given a file, name file.txt, out task is to write a bash script which print particular line from a file.. Content of file.txt: I love reading articles at geeks for geeks


Bash Command to Get Nth Line in a String?, hi i need to get the 3rd line in a string. actually, not sure if it's a string, list, array, put in bash command line without needing and requiring a script file. Because  The requirement is to print every 3rd line of the file. Let us consider a file with the following content. $ cat file AIX Solaris Unix Linux HPUX Ubuntu 1. Using awk : $ awk '!(NR%3)' file Unix Ubuntu NR variable contains the line number of the file in awk. If NR modulus 3 is equal to 0, it indicates the line is a multiple of 3.


Print nth line in a file - UNIX and Linux Forums, Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Is there an awk script that can easily perform the following operation?


How to keep only every nth line of a file, Edit: To actually get lines 1, 3, 6, 9, as in the example, rather than lines 1, 4, 7, 10, as I If you want to do it with a Bash script you can try: #!/bin/sh echo Please enter the file name read fname echo Please enter the Nth lines that you want