unix - using sed -n with variables -


i having log file a.log , need extract piece of information it. locate start , end line numbers of pattern using following.

start=$(sed -n '/1112/=' file9 | head -1) end=$(sed -n '/true/=' file9 | head -1) 

i need use variables (start,end) in following command:

sed -n '16q;12,15p' orig-data-file > new-file  

so above command appears like:

sed -n '($end+1)q;$start,$end'p orig-data-file > new-file 

i unable replace line numbers variables. please suggest correct syntax.

thanks, rosy

when realized how it, looking anyway line number file containing requested info, , display file line eof.

so, way.

with

pattern="pattern" input_file="file1" output_file="file2" 

line number of first match of $pattern $input_file can retrieved with

line=`grep -n ${pattern} ${input_file} | awk -f':' '{ print $1 }' | head -n 1` 

and outfile text $line eof. way:

sed -n ${line},\$p ${input_file} > ${output_file} 
  • the point here, way how can variables used command sed -n:

first witout using variables

sed -n 'n,$p' <file name> 

using variables

line=<n>; sed -n ${line},\$p <file name> 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -