extract substring using sed and regular expressions (regexp)
I would like to extract a number substring using sed.
echo "ifeelfat398pounds" | sed -n -e '/[0-9]/,/[0-9]/p' This is a very simple task but I have tried lots of combinations and have failed. I want to extract this. "398" |
isn't a good regexp
Code:
/\d+/ see www.regular-expressions.info EDIT: I tried your command, which returned the entire line, leading me to believe that sed's behavior is to return any line that matches, when given as you had it. I tried the substitution command, as follows: Code:
echo "ifeelfat398pounds" | sed -n -e 's/.*([0-9]+).*/\1/p' Code:
sed: -e expression #1, char 19: invalid reference \1 on `s' command's RHS Code:
echo "ifeelfat398pounds" | sed -n -e 's/([0-9]+)/&/p' |
Regex (unfortunately) ain't regex. For sed you'll need [:digit:] or [0-9].
And it'll look ugly - grep is a better tool for this (have a look at -o switch) |
you can also use perl
Code:
echo "ifeelfat398pounds" | perl -wlne 'print $1 if /(\d+)/' |
I think the first poster was using extended regular expressions with regular sed, so the parenthesis need to be escaped:
Code:
echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9][0-9]*\).*/\1/g' |
Jschiwal, thanks it works well.
echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9][0-9]*\).*/\1/g' |
no need for external tools
Code:
# a="ifeelfat398pounds" |
ghostdog -what is the difference between these two:
b=${a//[a-zA-Z]/} b=${a/[a-zA-Z]/} Is the first example equivalent to using the 'g' with sed? If so I've been looking for that. |
Quote:
|
Quote:
Try b=${a//[^0-9]/} instead. But even this would lead to problems if there were more than one set of numbers in the string. Something like "ifeelfat398poundsand15ounces." would give you an output of "39815". It would be nice if we could use full regex expressions inside of parameter substitution. Does anyone know if it's possible? |
ghostdog74 and David the H. that was nice. This is even better. I like bash scripting and am trying to utilize it to it's fullest. A goal is to minimize using other languages or external tools.
Thanks so much for the input so far. Lex |
Thanks ghostdog, I had tried to find that info before but couldn't find or make sense of what I was reading, I guess. The bash man-page is like an epic...
|
Quote:
Code:
# a="ifeelfat398poundsand15ounces" |
ghostdog74 is "shopt -s -o nounset" the same as this "# set --" from your example?
|
same problem, sort of
I am having the same problem....sort of. I want to extract a combination of character if they exist. If they don't exist, I want nothing. My problem is that if my pattern doesn't exist, I get the whole line returned.
if I have .....AA9999999999999999....., I want AA9999999999999999 if I have ............................, I want nothing. where AA9999999999999999 is 2 capital alphas followed by 16 numerics. I use 's/.*\(AA[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/' because \{16\} as a repeater doesn't work. |
All times are GMT -5. The time now is 12:18 PM. |