![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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` |
|||
| Google The UNIX and Linux Forums |