05-31-05 07: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
[ Post a follow-up to this message ]
|