remove newlines from a file
steve at ibctech.ca
Tue Sep 1 22:35:22 UTC 2009
George Davidovich wrote:
> On Tue, Sep 01, 2009 at 06:03:19PM +0000, Paul Schmehl wrote:
>> I found a sed tutorial once that did this, but I can't seem to find it
> You're probably thinking of "Useful One-Line Scripts for Sed":
> A good follow-up:
>> I have a file with multiple lines, each of which contains a single ip
>> followed by a /32 and a comma. I want to combine all those lines into
>> a single line by removing all the newline characters at the end of
>> each line.
>> What's the best/most efficient way of doing that in a shell?
> A sed solution would be
> sed -e :a -e '$!N; s/\n/ /; ta' my_file
> Other (easier to remember) solutions could include:
> tr -d '\n' < my_file
> tr '\n' ' ' < my_file
> echo $(cat my_file) # not so useless use of cat!
> paste -s my_file
> while read line; do
> joined="$joined $(echo $line)"
> done < my_file
> echo $joined
> Lots of options, of course. Even more with Perl.
Yeah, how 'bout Perl:
% perl -ne 's/\n/ /g; print;' < tests/ips.txt
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 3233 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20090901/ea0cb251/smime.bin
More information about the freebsd-questions