5 Replies Latest reply: Jun 7, 2010 5:07 AM by Jeffrey Young
Jeffrey Young Level 1 (0 points)
Im trying to do a search and replace on a text file using sed. I'm trying to replace the string "000W" with \r\n.

I've tryed:
sed 's/000W/\r\n/g' test.prn

Now wherever there is the string "000W" it is replaced by rn and not \r\n which is what I expected.

echo "test1;test2;test3;test4" | sed 's/;/\r\n/g'

Any clues?


MacBook, Mac OS X (10.5.7)
  • BobHarris Level 6 (17,634 points)
    Try this instead:

    echo "test1;test2;test3;test4" | perl -ne 's/;/ /g; print' | cat -vte

    Where the 'cat -vte' display ^M for the carriage return, and $ for the newline.

    So for your real goal, this should work:

    perl -ne 's/000W/ /g; print' test.prn
  • Tony T1 Level 6 (8,975 points)
    This will work:

    echo "test1;test2;test3;test4" | sed 's/;/'$' ''

    Two issues
    1) in sed, "However, if you are inserting a new line, don't use " " - instead insert a literal new line character" (see: http://www.grymoire.com/Unix/Sed.html section "Using newlines in sed scripts"
    2) in bash, to insert a carriage return, use $' '

    Words of the form $'string' are treated specially. The word expands to
    string, with backslash-escaped characters replaced as specified by the
    ANSI C standard. Backslash escape sequences, if present, are decoded
    as follows:
    a alert (bell)
    e an escape character
       form feed
    new line
    carriage return
          horizontal tab
    v vertical tab
    \ backslash
    \' single quote
    nn the eight-bit character whose value is the octal value
    nnn (one to three digits)
    xHH the eight-bit character whose value is the hexadecimal
    value HH (one or two hex digits)
    cx a control-x character

    The expanded result is single-quoted, as if the dollar sign had not
    been present.

    A double-quoted string preceded by a dollar sign ($) will cause the
    string to be translated according to the current locale. If the cur-
    rent locale is C or POSIX, the dollar sign is ignored. If the string
    is translated and replaced, the replacement is double-quoted.
  • Jeffrey Young Level 1 (0 points)
    Thanks, that worked.

    I now need to find out how to search for multiple "0.000E+00"s in my file elegantly using perl.
  • BobHarris Level 6 (17,634 points)
    I now need to find out how to search for multiple "0.000E+00"s in my file elegantly using perl.

    What do you want to do with the 0.000E+00 values? Change them to carriage return, line feeds?

    The perl -n option says to apply the perl script to each line of the input. The perl -e '...' says the '...' following the -e is a script to be executed.

    perl -ne 's/0.000E+00/ /g; print' input.file

    But if you want to do something else with those 0.000E+00 values, we are going to need some additional guidance.
  • Jeffrey Young Level 1 (0 points)
    I ended up reading a few man pages on Perl. I like it. I've written my first perl script to deal with converting a dump file (from a piece of equipment) to something MathCAD can import. I needed to discard the 0.000E+00 numbers plus a few other things. There were a few things that bailed me up but I found out why, and I now have a script file that executes blazingly fast - I really like this.
    Essentially three lines of perl script do the job after the end of the header is found:
    for ($hdrline=<$in>; $hdrline!~/000W/; $hdrline=<$in>){} # Positions the file pointer after the header
    s/\r\n/ /; # Replace <CR><LF> with ' '
    s/000W\sZR\d\s(\ds){4}/\r\n/; # Replace 000W ZRn n n n n with <CR><LF>
    s/(0\.000E\+00(\s{1,})){7}//; # Replace 7 x 0.000E+00 with nothing