Wednesday, 15 June 2011

shell - Bash script: reading numbers inside a text line -



shell - Bash script: reading numbers inside a text line -

i've got file catalog.dat containing lines following:

event_017_3916.gz

i need extract first number of line (017). number necessary launch programme (program.c), requires input numbers start (i.e., should used $ program.c 017). actually, need build command like:

program.c 017

inside shell script. problem is: how read 017 , set variable can utilize build command? lot help!

an awk solution:

awk -f_ '{print $2}' file

sets delimiter _ (-f_) , prints sec (print $2) field (where 017 stored)

to run trough file , save variable ($line contains 017):

while read line echo " > $line" program.c $line done < <(awk -f_ '{print $2}' file)

additionally can utilize xargs execute command with:

awk -f_ '{print $2}' file | xargs program.c

that executes program.c xyz every line in file, xzy extracted digit.

bash shell

No comments:

Post a Comment