Home > Software design >  Bash simple pattern extractor on text
Bash simple pattern extractor on text

Time:03-07

I'm stuck on a simple problem of finding a pattern in a string. I've never been comfortable with sed or regex in general.

I'm trying to get the number in the second column in one variable, and the number in the third column in another variable. The numbers are separated by tabs:

Here's what I have now :

while read line
do
    middle="$(echo "$line" | sed 's/([0-9] )\t\([0-9] \)\t([0-9] )\\.([0-9] )/\1/')"
    last="$(echo "$line" | sed 's/([0-9] )\t([0-9] )\t\([0-9] )\\.([0-9] \)/\1/')"
done

Here is the text :

11  1545    0.026666
12  1633    0.025444
13  1597    0.026424
14  1459    0.025634

I know there are simpler tools than 'sed', so feel free to put them to me in response. Thanks.

CodePudding user response:

This functionality is built into read.

while read first second third more; do
  …
done

By default, read splits its input into whitespace-separated fields. Each variable receives one field, except the last one which receives whatever remains on the line. This matches your requirement provided there aren't any empty columns.

CodePudding user response:

Use AWK to save yourself:

while read line
do
    middle="$(awk '{print $2}' <<< "$line")"
    last="$(awk '{print $3}' <<< "$line")"
done
  • Related