Wednesday, 15 January 2014

string - selecting a particular word from a file in shell scripts -



string - selecting a particular word from a file in shell scripts -

i have text file structure:

.... "/home/letizia/documents/spanishsegmentation/recordings/segmented/mfc/f001.0.rec" coge las hojas y las quemas todas en el fuego "/home/letizia/documents/spanishsegmentation/recordings/segmented/mfc/f002.0.rec" la liga de paz se reunio314201 para tratar el tema ....

and select "f0001.0" , "f0002.0".

i using:

id="f" if [[ "$line" == *recordings* ]] segment=`echo $line | grep -o $id.* | cutting -d'.' -f1-2` fi

but not work. mistake?

thank much in advance.

you need while loop:

while ifs= read -r line; id="f" if [[ "$line" =~ /recordings/ ]]; segment=$(echo $line | grep -o "$id.*" | cutting -d '.' -f1-2) echo "$segment" fi done < file.txt

results:

f001.0 f002.0

however, improve way utilize sed:

sed -n '/recordings/s#.*/\(f[^\.]*\.[^\.]*\).*#\1#p' file.txt

string shell grep

No comments:

Post a Comment