[plug] Using sed to remove \n from a text file.

Matthew Robinson matt at zensunni.org
Thu Jul 11 12:31:54 WST 2002


> To the best of my knowledge sed can't/won't do what you want.
>
> Try
> perl -i.bak -0p -e 's/-\n//g myfilename.txt

This probably ought to be written:

perl -i.bak -0p -e 's/-\n/, /g' myfilename.txt

The difference being this joins the lines and separates with a comma, which
seems more correct than concatenating the fields into one.  It also includes
the closing apostrophe which caused the previous version to fail.

>
> This will:
>  a. copy myfilename.txt to myfilename.txt.bak (-i.bak)
>  b. remove any/all "-\n"'s
>
>
> If the file is large (and reading the whole thing would be a bad thing
(tm))
> then try:
>
> -----
> #!/usr/bin/perl
>
> while (<>) {
> chop;
> if ($_ =~ /-$/) {
> print;
> } else {
> print "$_\n";
> }
> }
> -----
>
> perl script <myfile.txt > my_new_file.txt
>

This version does not produce the desired result as it joins the lines
without removing the hyphen.  It could be written more succinctly as, which
again joins with a comma:

#!/usr/bin/perl

while(<>) {
    s/-\n/, /g;
    print;
}

Hope this helps,

Matt

--
s&&!msfQ!&&s&$&utvK&&s&(Q)&\1!sfiupoB&&s&^&reverse Ibdlfs&e&s&^&#
&&s&$&#!uojsq&&s&(.)&chr(ord($1)-1)&ge&s&(.*)&reverse $1&see




More information about the plug mailing list