Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

head command

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-23-2007
sumesh.abraham
Guest
 
Posts: n/a
head command

Hi All,

How can the head command be used to extract only a particular line.
By default head -n filename displays the first n lines. I want only the nth line.
I couldn't get it from forum search.

Thanks,
Sumesh
Sponsored Links
    #2  
Old Unix and Linux 02-23-2007
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 7 November 2016, 6:31 AM EST
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 Posts
You can use sed or awk to get the nth line
For example, to display the 10th line

Code:
sed -n "10{p;q;}" file

or

Code:
awk -v ln=10 ' NR == ln { print; exit } ' file

Sponsored Links
    #3  
Old Unix and Linux 02-23-2007
sumesh.abraham
Guest
 
Posts: n/a
Anbu,
Thanks for the reply.

Could u please explain the following part

Code:
"10{p;q;}"

Thanks,
Sumesh
    #4  
Old Unix and Linux 02-23-2007
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 7 November 2016, 6:31 AM EST
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 Posts

Code:
"10{p;q;}"

p - print command
q - quit command
If the current line in pattern space is the tenth line of the input file then print that line and quit the sed script.If we wont quit the sed command, it will process the file till its end.
Sponsored Links
    #5  
Old Unix and Linux 02-23-2007
sumesh.abraham
Guest
 
Posts: n/a
Thanks Anbu. It worked. You are of great help to many.
Sponsored Links
    #6  
Old Unix and Linux 02-23-2007
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 5 December 2016, 11:37 AM EST
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
another way:


Code:
head -n filename | tail -1

Sponsored Links
    #7  
Old Unix and Linux 02-23-2007
sumesh.abraham
Guest
 
Posts: n/a
Yogesh,

I think by the code

Code:
head -n filename | tail -1

what you meant is

Code:
 head -<Required line number> filename | tail -1

I tried it and it worked!!

Thanks,
Sumesh
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix find and head command help partieboi37 Homework & Coursework Questions 11 04-22-2012 07:40 PM
head command with more than one file josegr Shell Programming and Scripting 4 06-09-2009 07:15 AM
alternative for head command nikhilneela UNIX for Dummies Questions & Answers 2 02-19-2009 08:39 AM
Simple Command (head) Question jbud UNIX for Dummies Questions & Answers 4 04-23-2006 10:16 PM



All times are GMT -4. The time now is 08:24 AM.