Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 1 Sep 2009 18:20:56 -0600 (MDT)
From:      Warren Block <wblock@wonkity.com>
To:        Steve Bertrand <steve@ibctech.ca>
Cc:        George Davidovich <freebsd@optimis.net>, freebsd-questions@freebsd.org
Subject:   Re: remove newlines from a file
Message-ID:  <alpine.BSF.2.00.0909011818190.7738@wonkity.com>
In-Reply-To: <4A9DA1AB.8020102@ibctech.ca>
References:  <F2B402210EF1C4F7331B41C2@utd65257.utdallas.edu> <20090901205201.GA6126@marvin.optimis.net> <4A9DA1AB.8020102@ibctech.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 1 Sep 2009, Steve Bertrand wrote:
> 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
>>> again.
>>
>> You're probably thinking of "Useful One-Line Scripts for Sed":
>>
>> http://sed.sourceforge.net/sed1line.txt
>>
>> A good follow-up:
>>
>> http://www.osnews.com/story/21004/Awk_and_Sed_One-Liners_Explained
>>
>>> 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

perl -pe 'chomp' myfile

is somewhat easier.  Works with Ruby, too.

-Warren Block * Rapid City, South Dakota USA



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.BSF.2.00.0909011818190.7738>