Sed and Regex

mromey

Registered
I'm having a very difficult time with regular expressions embedded in "sed". Regex is not this difficult in BBedit. Hear is what I am try to do.

1. I have multiple lines of data that looks like this:

32.60815684 49.75617397 162.6619038 -16.19942851 11.2 -2.026439909e-016

2. I need to format each line of data to look like these two lines of data:

RENDERER*TREE*$cube1*TRANSFORMATION*POSITION SET 32.60815684 49.75617397 162.6619038<br/>
RENDERER*TREE*$cube1*TRANSFORMATION*ROTATION SET -16.19942851 11.2 -2.026439909e-016<br/>

3. Currently I can only get sed to work when I don't have regular expressions in my command such as this:
#!/bin/sh
sed s
/mike
/name
/g original_file.xml > new_file.xml

4. How do I properly execute finiding tabs. All the command that I am used to in BB edit such as \t or \d or \w don't work properly. Realizing I'm missing something import to get this command to work, how can I get this type of command working:
#!/bin/sh
sed s
/(.+)\t(.+)\t(.+)\t(.+)\t(.+)\t(.+)\r
/RENDERER\*TREE\*\$cube1\*TRANSFORMATION\*POSITION \1 \2 \3<br/>\rRENDERER\*TREE\*\$cube1\*TRANSFORMATION\*ROTATION \4 \5 \6<br/>\r
/g original_file.xml > new_file.xml

5. In examples 3 and 4 I have also included a carrage return for legability, how do I get this to excute so that the uglyness of regex is subdued a bit.

Thanks again any help and guidance anyone can offer.
 
Personally, I prefer to move away from sed when the regex gets really long; if there's no problem with it, use perl. This would look like:

Code:
#!/usr/bin/perl

while( <> )
{
   /^(.+)\t(.+)\t(.+)\t(.+)\t(.+)\t(.+)$/;

   print "RENDERER*TREE*\$cube1*TRANSFORMATION*POSITION SET $1 $2 $3\n";
   print "RENDERER*TREE*\$cube1*TRANSFORMATION*ROTATION SET $4 $5 $6\n";
}

You would then just run perlscript infile > outfile replacing perlscript with whatever you call it.
 
I'm a big fan of regex power, but that just looks like you sneezed while your hands were on the keyboard...
Maybe that's what took you so long.

But, hey, since it's perl, TMTOWTDI...
 
Back
Top