Write a bash script to print a particular line from a file

26 Sep, 2017
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

Syntax in Bash Script
awk :$>awk '{if(NR==LINE_NUMBER) print $0}' file.txtsed :$>sed -n LINE_NUMBERp file.txthead :$>head -n LINE_NUMBER file.txt | tail -n + LINE_NUMBER
Here LINE_NUMBER is, 
which line number you want to printExamples:Print a line from single fileTo print 4th line from the file then we will run following commands. The output of following lines will be “articles”.awk :$>awk '{if(NR==4) print $0}' file.txtsed :$>sed -n 4p file.txt head :$>head -n 4 file.txt | tail -n + 4Print a line from multiple filesSuppose we have two files, file1.txt and file2.txt, We can use the above commands and print particular line from multiple files by ‘&’.awk:$>awk '{if(NR==4) print $0}' file1.txt & awk '{if(NR==4) print $0}' file2.txtsed:$>sed -n 4p file1.txt & sed -n 4p file2.txthead:$>head -n 4 file1.txt | tail -n + 4 & head -n 4 file2.txt | tail -n + 4This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
awk :$>awk '{if(NR==LINE_NUMBER) print $0}' file.txt
$>awk '{if(NR==LINE_NUMBER) print $0}' file.txt
sed :$>sed -n LINE_NUMBERp file.txt
$>sed -n LINE_NUMBERp file.txt
head :$>head -n LINE_NUMBER file.txt | tail -n + LINE_NUMBER
Here LINE_NUMBER is, 
which line number you want to printExamples:Print a line from single fileTo print 4th line from the file then we will run following commands. The output of following lines will be “articles”.awk :$>awk '{if(NR==4) print $0}' file.txtsed :$>sed -n 4p file.txt head :$>head -n 4 file.txt | tail -n + 4Print a line from multiple filesSuppose we have two files, file1.txt and file2.txt, We can use the above commands and print particular line from multiple files by ‘&’.awk:$>awk '{if(NR==4) print $0}' file1.txt & awk '{if(NR==4) print $0}' file2.txtsed:$>sed -n 4p file1.txt & sed -n 4p file2.txthead:$>head -n 4 file1.txt | tail -n + 4 & head -n 4 file2.txt | tail -n + 4This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
$>head -n LINE_NUMBER file.txt | tail -n + LINE_NUMBER
Here LINE_NUMBER is, 
which line number you want to print
Examples:
Print a line from single file
To print 4th line from the file then we will run following commands. The output of following lines will be “articles”.
awk :$>awk '{if(NR==4) print $0}' file.txtsed :$>sed -n 4p file.txt head :$>head -n 4 file.txt | tail -n + 4
awk :$>awk '{if(NR==4) print $0}' file.txt
$>awk '{if(NR==4) print $0}' file.txt
sed :$>sed -n 4p file.txt 
$>sed -n 4p file.txt 
head :$>head -n 4 file.txt | tail -n + 4
$>head -n 4 file.txt | tail -n + 4
Print a line from multiple files
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:$>awk '{if(NR==4) print $0}' file1.txt & awk '{if(NR==4) print $0}' file2.txtsed:$>sed -n 4p file1.txt & sed -n 4p file2.txthead:$>head -n 4 file1.txt | tail -n + 4 & head -n 4 file2.txt | tail -n + 4
awk:$>awk '{if(NR==4) print $0}' file1.txt & awk '{if(NR==4) print $0}' file2.txt
$>awk '{if(NR==4) print $0}' file1.txt & awk '{if(NR==4) print $0}' file2.txt
sed:$>sed -n 4p file1.txt & sed -n 4p file2.txt
$>sed -n 4p file1.txt & sed -n 4p file2.txt
head:$>head -n 4 file1.txt | tail -n + 4 & head -n 4 file2.txt | tail -n + 4
$>head -n 4 file1.txt | tail -n + 4 & head -n 4 file2.txt | tail -n + 4
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Linux-Unix
TechTips
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
Conditional Statements | Shell Script
Tail command in Linux with examples
Docker - COPY Instruction
scp command in Linux with Examples
How to Find the Wi-Fi Password Using CMD in Windows?
Docker - COPY Instruction
Setting up the environment in Java
How to Run a Python Script using Docker?
Running Python script on GPU.