The UNIX and Linux Forums  

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 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-25-2008 11:47 PM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 05:29 AM
extract a line from a file using the line number grandtheftander Shell Programming and Scripting 6 02-06-2008 03:12 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-20-2007 10:29 PM
Get line form file by line number corny Shell Programming and Scripting 3 08-25-2006 09:18 AM

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

Join Date: May 2008
Posts: 4
Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time.
I have tried sed, awk, head .. See my script

# Get randome line number from the file
#selectedline = `awk 'NR==$randomline {print $0}' $filename`
#awk 'NR==$randomline {print $0}' $filename
selectedline=`sed -n "${randomline}{p;q;}" $filename`
#sed -n "${randomline}{p;q;}" $filename
#selectedline=`head -$randomline $filename |tail -1`
#head -${randomline} $filename |tail -1
echo "Random line = $selectedline"
The problem is that after getting the desired line specified by randomline, the entire file is expanded in the variable selectedline. This the case only ,when assigned to a variable.

Need help
Reply With Quote
Forum Sponsor
  #2  
Old 05-01-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Code:
# one way
lin=4
file=/path/somefile
sed -n $lin,"$lin"p $file | read myvariable
# another way
myvariable=`awk -v line=$lin "NR==line" $file`
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 12:21 PM.


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