PDA

View Full Version : Regexp help?



linuxgeekery
August 18th, 2008, 11:13 PM
Like many, I'm a regex noob and I need a little sed (or possibly awk) help.

I have a bunch of data formatted like:

This 01 Is 02 An 03 Example 04

and I need it formatted like:
This 01
Is 02
An 03
Example 04

I would be able to figure this out myself, but sed doesn't seem to want to work with newlines.

Thanks!

mssever
August 19th, 2008, 12:24 AM
Like many, I'm a regex noob and I need a little sed (or possibly awk) help.
How about Ruby?

echo 'This 01 Is 02 An 03 Example 04' | ruby -pe "gsub(/([0-9]+) /,'\1' + \"\n\")"

pdwerryhouse
August 19th, 2008, 02:00 AM
This works for me:


echo "This 01 Is 02 An 03 Example 04" | sed 's/\([0-9]\+\) \?/\1\n/g'

ghostdog74
August 19th, 2008, 02:15 AM
things can be done easily without regexp


awk '{ for(i=1;i<NF;i=i+2) { print $i,$(i+1) }}' file

linuxgeekery
August 19th, 2008, 03:47 AM
This works for me:


echo "This 01 Is 02 An 03 Example 04" | sed 's/\([0-9]\+\) \?/\1\n/g'

Weird. Both the awk and ruby examples work, but yours doesn't.

Thanks!

ghostdog74
August 19th, 2008, 03:51 AM
unless your data is really structured like that, using regexp and checking for digits is less flexible than going over the fields and implementing a newline for every 2 words.