0

I have a file containing on each line a string of the form

string1.string2:\string3{string4}{number}

and what I want to extract is the number. I've searched and tried for a while to get this done using sed or bash, but failed. Any help would be much appreciated.

Edit 1: The strings may contains numbers.

1
  • Yep, we certainly are... Commented Sep 6, 2012 at 5:43

4 Answers 4

5
$ echo 'string1.string2:\string3{string4}{number}' |\
  cut -d'{' -f3 | cut -d'}' -f 1
number
0
2

Using sed:

sed 's/[^}]*}{\([0-9]*\)}/\1/' input_file 

Description:

[^}]*}      : match anything that is not } and the following }
{\([0-9]*\)}: capture the following digits within {...}
/\1/        : substitute all with the captured number
0

Use grep:

grep -o '\{[0-9]\+\}' | tr -d '[{}]'
3
  • 1
    What if a number is in one of the strings? In addition, grep selects lines, not fields in those lines.
    – user647772
    Commented Sep 5, 2012 at 13:33
  • Yes, the strings themselves may have numbers. I will edit the question to make this clear. Commented Sep 5, 2012 at 13:37
  • Fixed it. Only match numbers within curly braces, and post-remove them.
    – Thor
    Commented Sep 5, 2012 at 13:38
0

In bash:

sRE='[[:alnum:]]+'
nRE='[[:digit:]]+'
[[ $str =~ $sRE\.$sRE:\\$sRE\{$sRE\}\{($nRE)\} ]] && number=${BASH_REMATCH[1]}

You can drop the first part of the regular expression, if your text file is sufficiently uniform:

[[ $str =~ \\$sRE{$sRE}{($nRE)} ]] && number=${BASH_REMATCH[1]}

or even

[[ $str =~ {$sRE}{($nRE)} ]] && number=${BASH_REMATCH[1]}
2
  • I think sRE should be [[:alnum:]]+, and I need to escape the curly braces to make this work here.
    – Thor
    Commented Sep 5, 2012 at 14:03
  • Right on both counts. I wondered about the braces, but was too lazy to check.
    – chepner
    Commented Sep 5, 2012 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.