head command | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

head command

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 02-23-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 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 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 02-23-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 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 02-23-2007
sumesh.abraham
Guest
 
Posts: n/a
Thanks Anbu. It worked. You are of great help to many.
Sponsored Links
    #6  
Old 02-23-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
another way:


Code:
head -n filename | tail -1

Sponsored Links
    #7  
Old 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:40 PM
head command with more than one file josegr Shell Programming and Scripting 4 06-09-2009 06:15 AM
alternative for head command nikhilneela UNIX for Dummies Questions & Answers 2 02-19-2009 07:39 AM
Simple Command (head) Question jbud UNIX for Dummies Questions & Answers 4 04-23-2006 09:16 PM



All times are GMT -4. The time now is 11:43 PM.