Unix Programming - Regex substitution

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > Regex substitution





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 Regex substitution
Russell Shaw

2005-05-31, 2:48 am

Hi,
In a file, i have lines of fairly random structure like:

123 abc size=27 xyz size=48 opx

I'm trying to get sed to print out: size=27 size=48

However, i can only print the whole line. How do i get sed
to print out only the matched regions? If it can't do it, are
there any other tools simpler than PERL for doing that?

I tried: sed -n "s/size=\([0-9]*\)/size=\1/gp" input.txt
Pascal Bourguignon

2005-05-31, 2:48 am

Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:

> Hi,
> In a file, i have lines of fairly random structure like:
>
> 123 abc size=27 xyz size=48 opx
>
> I'm trying to get sed to print out: size=27 size=48
>
> However, i can only print the whole line. How do i get sed
> to print out only the matched regions? If it can't do it, are
> there any other tools simpler than PERL for doing that?


No, sed is Turing complete, you don't need anything else.


> I tried: sed -n "s/size=\([0-9]*\)/size=\1/gp" input.txt


You aren't changing anything here!

echo '123 abc size=27 xyz size=48 opx'|\
sed -n -e 's/ / /g' -e 's/\(.*\)/ \1 /' -e 's/ [^ =]\+ / /gp'
size=27 size=48

Perhaps what you want is:

cat >input.txt <<EOF
line without size
size alone:
size=1
size at the beginning:
size=2 blah
size at the end:
blah size=3
size in the middle:
blah size=4 blah
etc..
size=5 hihi size=6 haha size=7
foo size=8 bar baz size=9 quux
si ze=5 hi hi size=6 ha ha size=7
fo o size=8 bar baz size=9 qu ux
size=10 size=11
EOF

sed -n '
/size=[0-9]\+/{
s/ / /g
s/\(.*\)/ \1 /
s/ [^ =]\+ / /g
s/ \+/ /g
s/^ //
s/ $//
p}' input.txt

size=1
size=2
size=3
size=4
size=5 size=6 size=7
size=8 size=9
ze=5 size=6 size=7
size=8 size=9
size=10 size=11


--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
William Park

2005-05-31, 6:03 pm

Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
> Hi,
> In a file, i have lines of fairly random structure like:
>
> 123 abc size=27 xyz size=48 opx
>
> I'm trying to get sed to print out: size=27 size=48
>
> However, i can only print the whole line. How do i get sed
> to print out only the matched regions? If it can't do it, are
> there any other tools simpler than PERL for doing that?
>
> I tried: sed -n "s/size=\([0-9]*\)/size=\1/gp" input.txt


Let shell do the work. That's what it's for.

set -- 123 abc size=27 xyz size=48 opx
echo ${*|/=}
or
set -- 0 123 abc size=27 xyz size=48 opx
while shift; do
[[ $1 == *=* ]] && echo -n "$1 "
done
echo
or
echo 123 abc size=27 xyz size=48 opx | tr -s ' ' '\n' | grep '=' | tr '\n' ' '
echo
or
echo 123 abc size=27 xyz size=48 opx | grep -o '[^ ]*=[^ ]*' | tr '\n' ' '

I think the first is the best solution of all. :-) Explanation is
${var|/regex} -- return items containing 'regex'

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
James Antill

2005-05-31, 8:51 pm

On Tue, 31 May 2005 09:25:28 +0200, Pascal Bourguignon wrote:

> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
>
>
> No, sed is Turing complete, you don't need anything else.


> Perhaps what you want is:


> sed -n '
> /size=[0-9]\+/{
> s/ / /g
> s/\(.*\)/ \1 /
> s/ [^ =]\+ / /g
> s/ \+/ /g
> s/^ //
> s/ $//
> p}' input.txt


Help ... my eyes are bleeding.

This is "simpler than perl"?

perl -nle '
my @a = m/(size=\d+)/g;
print join(" ", @a) if (@a)' input.txt

--
James Antill -- james@and.org
http://www.and.org/vstr/httpd

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com