Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-26-2013, 02:26 PM   #1
LQ Newbie
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Why sed is unable to remove/replace new line/'\n'

Hi there,
I am unable to replace or remove \n or new line in my source files. I will be getting 2000 files daily and I need to remove new lines from that files instantly.
Kindly suggest me workaround..

Thanks in Advance.

Old 06-26-2013, 03:47 PM   #2
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Although it is possible to do that with sed, you're better of with tr for the job:

tr -d '\n' < myfile >
this will delete newlines from myfile and save the output to
Old 06-26-2013, 04:20 PM   #3
Senior Member
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
this is a line with a new line
this is a line that I would like to join
with the next line
sed ':a;/would like to join$/{N;s/\n/ /;ba}' input
I cheated

the detail is probably in here

somewhere ..
Old 06-26-2013, 05:01 PM   #4
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220
As much as I like all things sed, as kubamaru suggests, tr is a better tool in this situation.
Old 06-26-2013, 05:19 PM   #5
Senior Member
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444

but I hate googling, finding a thread that looks like what I need and is marked solved only to find the solution doesn’t quite fit what I needed.

Just trying to fix the net
Old 06-26-2013, 06:39 PM   #6
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 223Reputation: 223Reputation: 223
This page might be useful. Plenty of examples on how to remove newlines with several tools.
1 members found this post helpful.
Old 06-27-2013, 12:38 AM   #7
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
It looks like that it should be easy with sed because we have used substitute and the replacing character so it should be
sed 's/\n/\t/g' filename or sed 's/\n/ /g' filename or sed 's/\n//g' filename
but when you will try these solutions, none of these will work because to change line you have to define all these
    :a create a label 'a'
    N append the next line to the pattern space
    $! if not the last line, ba branch (go to) label 'a'
    s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can)
But it does not like that if it is not done in sed, it can't be done. There are other several and much easy ways.
You have to replace new line with another character, in these examples I am replacing it with space.
If you do this using perl in a way like sed,
perl -p -e 's/\n/ /' filename.txt
with tr
tr '\n' ' ' < filename.txt
Another way with paste,
but here is some limitation, if you replace new line with one or two character(like space or tab), it is easy to use, if there are multiple charcters there can be some problem.
paste -s -d ' ' filename.txt
Old 06-27-2013, 03:31 AM   #8
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
sed variant:
sed -i.bak ':begin;$!N;s/\n//;tbegin' file
sed -i.bak '{:q;N;s/\n//g;t q}' file
But as everybody have already mentioned, tr is easy
tr -d '\n' < file > newfile
Old 06-28-2013, 07:56 AM   #9
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
sed can't do this by default because of the way it processes lines. The process goes like this:

1) The first line of input is taken into the pattern buffer, with the trailing newline removed from it.

2) All command expressions are applied to the contents of the buffer, in order.

3) The modified string is printed to stdout (unless -n is used), with a new newline re-appended to it.

4) It goes on to the next line and processes that.

Since there's never a newline in the pattern buffer, there's no way to act on it.

Now, as shown above, it can be done with some of the more advanced multi-line commands. These work by appending extra lines (either the next one in the input, or the contents of the hold buffer) to the contents of the pattern buffer, with newline characters between them. Then once there are newlines in the buffer, you can operate on them as you expect.

But the code flow of sed when doing this kind of thing can be a real headache. Usually it's much easier to just use a different tool entirely.

Here are a few useful sed references:
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace a line in file used sed command vin_2 Programming 1 10-17-2012 02:04 AM
[SOLVED] SED replace line after match r00t Linux - Newbie 12 05-31-2012 07:17 AM
[SOLVED] sed help - replace line feed with different character bradvan Programming 7 04-22-2012 11:31 PM
[SOLVED] Replace a line using sed finekevin Linux - Newbie 6 02-23-2012 01:14 PM
SED - replace line after substing rany Linux - Newbie 2 07-02-2009 01:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration