user1835630
user1835630

Reputation: 261

selecting a particular word from a file in shell scripts

I have a text file with this 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 I would like to select "F0001.0" and "F0002.0".

I am using:

     ID="F"
     if [[ "$LINE" == *Recordings* ]]
     then        
     SEGMENT=`echo $LINE | grep -o $ID.* | cut -d'.' -f1-2`
     fi

but it does not work. Where is the mistake?

Thank you very much in advance.

Upvotes: 0

Views: 2179

Answers (2)

Steve
Steve

Reputation: 54402

You need a while loop:

while IFS= read -r line; do
    id="F"
    if [[ "$line" =~ /Recordings/ ]]; then

        segment=$(echo $line | grep -o "$id.*" | cut -d '.' -f1-2)
        echo "$segment"
    fi
done < file.txt

Results:

F001.0
F002.0

However, a better way would be to use sed:

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

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249153

Try using sed instead:

sed -n 's@^".*/Recordings/.*/\(.*\)"$@\1@p' file.txt

A quick walkthrough:

  1. -n: don't print anything unless specifically requested (by the final p).
  2. s@: substitute the part until the next @ with the part until the one after that.
  3. ^".*/Recordings/.*/\(.*\)"$: match a line which starts and ends with double-quotes, contains /Recordings/, and eat everything until the last slash.
  4. \1: replace the matched string with the last part (which we captured in parentheses).

Upvotes: 2

Related Questions