The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cutting a section of a big file yoavbe Shell Programming and Scripting 6 07-21-2008 11:00 AM
Using Sed to duplicate a section of a file.... nick26 Shell Programming and Scripting 2 05-01-2008 08:18 AM
sed & awk--get section of file based 2 params Andy Cook Shell Programming and Scripting 12 12-01-2006 12:28 PM
getting error 0403-016 Cannot find or open the file while reading a long line karthee Shell Programming and Scripting 2 06-04-2005 09:00 PM
New Section kapilv Post Here to Contact Site Administrators and Moderators 3 10-31-2001 06:57 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-13-2005
Registered User
 

Join Date: Sep 2001
Location: UK
Posts: 25
help find a section line of a file

hi,

I have a 20 line file.

I need a command which will brinf back a specific line based upon the line number I enter.

e.g. the file looks like this and is called file1

jim is a man
john is a woman
james is a man
wendy is a woman
lesley is a woman

i want a command that will output any one of this lines based upon the number i pass it as parameter.

eg.

$ ./script file1 2 (script being the command)

will show

$ john is a woman

any help will be much appreciated

thanks in advance

Suresh
__________________
All I wanna o is live like the kings do.
All I wanna be is smarter than my enemy.
All I wanna have is everythng I haven't got.
All I wanna take is everything they make.
Reply With Quote
Forum Sponsor
  #2  
Old 01-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,653
sed -n 2p file1
should do it.
Reply With Quote
  #3  
Old 01-13-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Alternatively,
awk NR==2 file1
Reply With Quote
  #4  
Old 01-13-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by sureshy
i want a command that will output any one of this lines based upon the number i pass it as parameter.

eg.

$ ./script file1 2 (script being the command)
Using either of these two methods within a script would be easy, using positional parameters
Code:
#!/bin/sh
sed -n $2p $1
awk NR==$2 $1
Take a look at your shells manual page to learn more about positional parameters and passing arguments to scripts.

You could also implement error checking by ensuring that the file passed exists (look at the "test" or "[" builtins/commands)....

Cheers
ZB
Reply With Quote
  #5  
Old 03-10-2005
Registered User
 

Join Date: Sep 2001
Location: UK
Posts: 25
Sorry this is late..


Many Thanks, much appreciated.
__________________
All I wanna o is live like the kings do.
All I wanna be is smarter than my enemy.
All I wanna have is everythng I haven't got.
All I wanna take is everything they make.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0