Regex substitution
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Regex substitution




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Regex substitution  
Russell Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-05 07: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





[ Post a follow-up to this message ]



    Re: Regex substitution  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: Regex substitution  
William Park


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-05 11: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





[ Post a follow-up to this message ]



    Re: Regex substitution  
James Antill


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-01-05 01:51 AM

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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:37 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register