1754417 Members
2737 Online
108813 Solutions
New Discussion юеВ

how to paser this string

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

how to paser this string

Hi..guru..

I have not script for a while....and need your help again..

say if I have a line like this..

list="Only in /usr/local/bin Only in /usr/lib/bin Only in /home/smith"

I parse that one string into 3 strings such as

Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith

I used to know how to do this...

please help out!
17 REPLIES 17
James R. Ferguson
Acclaimed Contributor
Solution

Re: how to paser this string

Hi Gemini:

# echo ${list} | perl -nle 'print $1 while /(Only in\s+\S+)/g'

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: how to paser this string

Hi (again):

...and if you want a (longer) 'awk' solution:

# echo ${list} | awk '{while (match($0,/Only in [A-Za-z\/]+/)) {S=substr($0,RSTART,RLENGTH);print S;$0=substr($0,RLENGTH)}}'

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: how to paser this string

We can help you, but it would be best if on top of the example (which is a great start!) you could help describe how the string is build.

Speficifcally... Will a new chunk always start with space+"Only in", or just "Only", or...

$ echo $list | perl -pe 's% (Only in)%\n$1%g'
Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith

If it always starts with whatever the first chunk starts with, and there is a space, then you could use:

$ echo $list | perl -pe '$x=substr($_,0,5); s/\s$x/\n$x/g'
Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith
$


Hein.


Gemini_2
Regular Advisor

Re: how to paser this string

After I read everyone's reply, I dont feel as bad. the Awk command is soo long, but very educational. thank you.

I would like to try the perl command, but, I am still not getting this one quite right, please help me.

the input is

Only in /usr/local/bin: a.config Only in /usr/lib/bin: b.conf Only in /home/smith: c.conf


Notice there is no "space" before "Only in", but now, I add the file name...how do I include the file name....
James R. Ferguson
Acclaimed Contributor

Re: how to paser this string

Hi (again) Gemini:

OK, given:

# list="Only in /usr/local/bin: a.config Only in /usr/lib/bin: b.conf Only in /home/smit
h: c.conf"

...then:

# echo ${list} | perl -nle 'print $1 while /(Only in\s+\S+\s\S+)/g'

...yields:

Only in /usr/local/bin: a.config
Only in /usr/lib/bin: b.conf
Only in /home/smith: c.conf

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to paser this string

Hi:

...and for similar changes to the 'awk':

# echo ${list} | awk '{while (match($0,/Only in ([A-Za-z\/\:]+ [A-Za-z\.]+ *)/)) {S=substr($0,RSTART,RLENGTH);print S;$0=substr($0,RLENGTH)}}'

Note, of course, that with the 'awk' snippet, filenames containing numbers or special characters would fail the match. I'll leave that as an exercise for you!

The Perl solutions using '\s' for a whitespace and '\S' for a nonwhitespace is easier (as Perl is!). For that matter, Hein's solutions are quite elegant too, and required no changes.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: how to paser this string

Yet another (shorter) awk solution :)

# echo $list | awk -F"Only" '{for(i=1;i<=NF;++i) if($i) print FS$i}'

~cheers
James R. Ferguson
Acclaimed Contributor

Re: how to paser this string

Hi Sandman:

That's a VERY elegant 'awk' solution! Shorter AND sensitive only to matching the keyword "Only". I always learn better ways with 'awk' and 'sed' reading your contributions.

/* NO POINTS for this post, please */

Regards!

...JRF...
Sandman!
Honored Contributor

Re: how to paser this string

That coming from the master (JRF) himself is quite the compliment.

:D