Unix/Linux

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • search and replace end of line?

    6 answers - 570 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I have browsed old messages but I have not found any good answer to how
    to substitute a end of line character.
    This is what I want to do:
    I have a file that ends like
    myArray[someBigSize] = {
    0x78u, 0x0f
    };
    So I want sed to find the eol and replace it so that the file looks
    like
    0x78u, 0x0fu
    };
    i.e. the 'u' is added but the rest is keept intact.
    This is what I tried to do
    sed "s/\n\}\;/u\n\}\;/" < myFile.c
    But this wont do it for me?
    Why?
  • No.1 | | 796 bytes | |

    di98mase wrote:
    Hi,

    I have browsed old messages but I have not found any good answer to how
    to substitute a end of line character.

    This is what I want to do:

    I have a file that ends like
    myArray[someBigSize] = {
    .
    .

    0x78u, 0x0f
    };

    So I want sed to find the eol and replace it so that the file looks
    like

    0x78u, 0x0fu
    };

    i.e. the 'u' is added but the rest is keept intact.

    This is what I tried to do

    sed "s/\n\}\;/u\n\}\;/" < myFile.c

    But this wont do it for me?

    Why?

    The virtual end of line marker is the $ character.

    s/$/u/

    if you just want to change certain files that, e.g., have a comma
    somewhere in the line

    /,/s/$/u/

    Janis
  • No.2 | | 949 bytes | |

    di98mase wrote:
    Hi,

    I have browsed old messages but I have not found any good answer to how
    to substitute a end of line character.

    This is what I want to do:

    I have a file that ends like
    myArray[someBigSize] = {
    .
    .

    0x78u, 0x0f
    };

    So I want sed to find the eol and replace it so that the file looks
    like

    0x78u, 0x0fu
    };

    i.e. the 'u' is added but the rest is keept intact.

    This is what I tried to do

    sed "s/\n\}\;/u\n\}\;/" < myFile.c

    But this wont do it for me?

    Why?

    If you want to search only the last 3 lines for the replacement, you
    may try:

    perl -0777pe 's/(\S)([\n\s]*};\s*)\z/\1u\2/' myfile.txt

    which slurps in the whole file as a string and then modifies it, or

    tac myfile.txt | sed '3s/$/u/' | tac

    looks clumsy, but it should work, :-)

    Xicheng

  • No.3 | | 370 bytes | |

    Hi Xicheng,

    your pearl command did it! I must say that I have a bit hard to
    understand exactly what it does but it works. I did try Janis
    suggestions but it seems that sed only "acts" on each line at a time
    thus I can not search for a EL on one line + }; that comes on the next
    line

    Thanks Janis and Xicheng

    /Sebastian

  • No.4 | | 904 bytes | |

    9 Mar 2006 23:09:31 -0800, "di98mase" <di98mase@hotmail.comwrote:

    >Hi Xicheng,
    >
    >your pearl command did it! I must say that I have a bit hard to
    >understand exactly what it does but it works. I did try Janis
    >suggestions but it seems that sed only "acts" on each line at a time
    >thus I can not search for a EL on one line + }; that comes on the next
    >line
    >


    The default action of 'sed' is to read a line at a time and work
    with that but it can deal with multiple lines just fine;

    sed -n '$!N;s/\n};/u&/;P;D'

    Not sure what you're trying to do exactly but the above will
    do what you were trying to do with the 'sed' script you posted.

    bestwishes
    laura


    >Thanks Janis and Xicheng
    >
    >/Sebastian
    >
  • No.5 | | 531 bytes | |

    Xicheng wrote:

    If you want to search only the last 3 lines for the replacement, you
    may try:

    perl -0777pe 's/(\S)([\n\s]*};\s*)\z/\1u\2/' myfile.txt

    The \s character class includes the \n character so [\n\s] is redundant and \1
    and \2 backreferences should only be used in regular expressions, so:

    perl -0777pe 's/(\S)(\s*};\s*)\z/$1u$2/' myfile.txt

    without the capturing parentheses:

    perl -0777pe 's/(?<=\S)(?=\s*};\s*\z)/u/' myfile.txt

    John
  • No.6 | | 892 bytes | |

    John W. Krahn wrote:
    Xicheng wrote:

    If you want to search only the last 3 lines for the replacement, you
    may try:

    perl -0777pe 's/(\S)([\n\s]*};\s*)\z/\1u\2/' myfile.txt

    The \s character class includes the \n character so [\n\s] is redundant and \1
    and \2 backreferences should only be used in regular expressions, so:

    yep, I initially used only [\n ], and later I found I could add \s to
    the character-class, and thus didnt notice this redundence. :-) the \1
    \2 thing has been an old news in perl.misc. but anyway, it corrected me
    some old thoughs about this stuff .:-)

    Best,
    Xicheng

    perl -0777pe 's/(\S)(\s*};\s*)\z/$1u$2/' myfile.txt

    without the capturing parentheses:

    perl -0777pe 's/(?<=\S)(?=\s*};\s*\z)/u/' myfile.txt
    >
    >
    >

    John

Re: search and replace end of line?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "Unix/Linux"

EMSDN.COM