Unix Shell - sed regexp question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2004 > sed regexp question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sed regexp question
seand

2004-05-22, 10:28 pm

Hi all,

I need a regexp pattern can catch literal string "ver: 1234 abcd" or "1234
abcd", and the output should be "1234 abcd" or "abcd" respectively, but the
code below doesn't work. where am I doing wrong?
# backslashs have been removed for simplicity
echo "ver: 1234 abcd" |
sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' #expecting the
output is "1234 abcd" here, but gets "abcd"
echo "1234 abcd" |
sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' # output is
"abcd", correct.

TIA


-s


Barry Margolin

2004-05-22, 10:28 pm

In article <kqwrc.91618$Zxc.6797@news04.bloor.is.net.cable.rogers.com>,
"seand" <seand@internet.net> wrote:

> Hi all,
>
> I need a regexp pattern can catch literal string "ver: 1234 abcd" or "1234
> abcd", and the output should be "1234 abcd" or "abcd" respectively, but the
> code below doesn't work. where am I doing wrong?
> # backslashs have been removed for simplicity
> echo "ver: 1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' #expecting the
> output is "1234 abcd" here, but gets "abcd"


Try anchoring the match at the beginning of the line with the ^ regexp.

> echo "1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' # output is
> "abcd", correct.
>
> TIA
>
>
> -s


--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
rakesh sharma

2004-05-22, 10:28 pm

"seand" <seand@internet.net> wrote in message news:

>
> I need a regexp pattern can catch literal string "ver: 1234 abcd" or "1234
> abcd", and the output should be "1234 abcd" or "abcd" respectively, but the
> code below doesn't work. where am I doing wrong?
> # backslashs have been removed for simplicity
> echo "ver: 1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' #expecting the
> output is "1234 abcd" here, but gets "abcd"
> echo "1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' # output is
> "abcd", correct.
>


remove the global flag(/g) in the s/// command:

Or, can use this:

sed -e 's/[[:space:]]\+/\n/;s/.*\n//'
Stephane CHAZELAS

2004-05-24, 7:32 am

2004-05-21, 23:38(+00), seand:
> Hi all,
>
> I need a regexp pattern can catch literal string "ver: 1234 abcd" or "1234
> abcd", and the output should be "1234 abcd" or "abcd" respectively, but the
> code below doesn't work. where am I doing wrong?
> # backslashs have been removed for simplicity
> echo "ver: 1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' #expecting the
> output is "1234 abcd" here, but gets "abcd"
> echo "1234 abcd" |
> sed -e's/([a-z]+:[[:space:]]*)|([0-9a-z]+[[:space:]]*)//g' # output is
> "abcd", correct.


"|", "+" and "(" are extended regexp operators. sed implements basic
regexps. Some seds have "\|" however.

[:space:] includes vertical spaces (\r, \n, \f, \v...), you
probably meant [:blank:].

sed -e 's/^[a-z]\{1,\}:[[:blank:]]*//;t' \
-e 's/^[0-9a-z]\{1,\}[[:blank:]]*//'
(not tested)

Note that some old seds may not support "\{".

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com