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
extract a line from a file using the line number grandtheftander Shell Programming and Scripting 7 01-22-2009 10:24 AM
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
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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-01-2008
zambo zambo is offline
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
  #2 (permalink)  
Old 05-01-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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`
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 02:24 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