The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using grep to extract line number mskarica Shell Programming and Scripting 8 06-26-2008 02:47 AM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 08:29 AM
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 01:39 PM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM
Get line form file by line number corny Shell Programming and Scripting 3 08-25-2006 12:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2006
grandtheftander grandtheftander is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 8
extract a line from a file using the line number

Hello,
I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4).
Extra Notes:
-I am working in a bash script.
-The file size is always variable, so I couldn't use head or tail.
#file that I am evaluating
Code:
hello
hey
hi
there <-line that i want
how
are
you 
doing
  #2 (permalink)  
Old 08-02-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Use sed. Check the 'p' command in particular.
  #3 (permalink)  
Old 08-02-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
  
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 521
If you want a particular line number, say line 4, using head and tail commands:
Code:
 head -4 filename | tail -1
Using sed:
Code:
 sed 4!d filename
  #4 (permalink)  
Old 08-03-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
You can use awk

awk 'NR==4{print $0}' filename

would print the 4th line for you.
  #5 (permalink)  
Old 08-03-2006
grandtheftander grandtheftander is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 8
Thank you everyone for responding. I went with the head to tail command. I previously didn't think the head/tail command would work but thank you for showing me the correct usage.
  #6 (permalink)  
Old 08-03-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
No need to explicitly print in the awk solution, it can simply be: awk 'NR==4' filename
  #7 (permalink)  
Old 02-06-2008
ashishdugar ashishdugar is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 1
Quote:
Originally Posted by grandtheftander View Post
Hello,
I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4).
Extra Notes:
-I am working in a bash script.
-The file size is always variable, so I couldn't use head or tail.
#file that I am evaluating
Code:
hello
hey
hi
there <-line that i want
how
are
you 
doing

head -4 | awk '{ print $1 }' < filename | tail -1
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0