LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-19-2007, 06:55 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Smile Sed remove trailing newline


Hello everyone. I have a text composed of many lines. Please, can you tell me how can I get rid of the newlines and join the text in one big line? Thank you
 
Old 02-19-2007, 07:00 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Just after I posted I found this
Code:
sed -n 'H;${g;s/\n/ /g;p}'
If you have a better (simpler) way I would be thankful if you would post it
 
Old 02-19-2007, 07:03 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
slightly laterally

xargs < file
 
Old 02-19-2007, 09:20 AM   #4
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thumbs up

Thank you very much. Your example is (slightly) simpler.. But I will decide to use it because it may prove to be.. uh, more portable, yes, that's it ) Again, thank you.
 
Old 02-19-2007, 09:22 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 615

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
This will remove all newlines:
Code:
cat file | tr -d '\n'
If you want to find regex matches over whole file with sed, it may be useful to translate newlines to zero-chars '\0'. Then, after filtering, you will be able to translate it back:
Code:
cat file | tr '\n' '\0' | filtering | tr '\0' '\n'
PS: bigearsbilly: Really cool! But I encounter an error when `file' contain unmatched single quote (').

Last edited by firstfire; 02-19-2007 at 09:32 AM.
 
Old 02-19-2007, 09:51 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
I never said it was perfect!

Ok so it's a rubbish solution except for simple examples
 
Old 02-20-2007, 12:11 PM   #7
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
I seem to recall using

Code:
sed -ni 's/\n/ /g'
See if that works.
Joel
 
Old 02-20-2007, 03:22 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
For every sed one liner, there is a perl one:

Code:
perl -pe 'chomp;' input_file > output_file
 
Old 02-20-2007, 10:12 PM   #9
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by firstfire
This will remove all newlines:
Code:
cat file | tr -d '\n'
If you want to find regex matches over whole file with sed, it may be useful to translate newlines to zero-chars '\0'. Then, after filtering, you will be able to translate it back:
Code:
cat file | tr '\n' '\0' | filtering | tr '\0' '\n'
PS: bigearsbilly: Really cool! But I encounter an error when `file' contain unmatched single quote (').

Why to have the cat ?

Isnt that UUOC
 
Old 02-20-2007, 11:49 PM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 615

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
Quote:
Originally Posted by kshkid
Why to have the cat ?

Isnt that UUOC
You can do the same without `cat' if you want:

Code:
tr -d '\n' < file
What is UUOC?
 
Old 02-21-2007, 01:47 AM   #11
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Dual boot :: Windows vista 64-bit | Kali linux 64bit multi lib
Posts: 151

Rep: Reputation: 33
Quote:
Originally Posted by firstfire
What is UUOC?
I have no idea but, Unnecessary Use Of Cat would match :P
 
Old 02-21-2007, 03:18 AM   #12
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
that's

Code:
Useless Use Of Cat - UUOC
 
Old 02-21-2007, 07:27 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
While I agree you can do that w/o cat this example really is a minor nit IMHO. A better example of UUOC would be those situations where you "cat file|awk" or "cat file|sed" where you can "awk file" or "sed -i file".
 
Old 02-21-2007, 07:42 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
UUOC indeed, but i would say the first example
is easier to scan and edit:

cat file | sort -k1,2rn | cut -d\ -f3-9 | grep -v INFO

sort -k1,2rn < file | cut -d\ -f3-9 | grep -v INFO



IMnsHO
 
Old 02-21-2007, 08:40 AM   #15
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 32
Looks like you've got a solution already, but I thought I'd mention the fmt command, which is aimed at text formatting - unfortunately (at least on HP-UX!) it doesn't have an option to fill/join lines to an arbitrary width. But it's occasionally useful to fill lines to a certain width... in particular, in vi typing !}fmt will line-fill the current paragraph, which is very handy when entering a lot of text.

"xargs < file" is the nicest one-liner I've seen in a long time :-)
 
  


Reply


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
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 4 03-19-2014 01:28 PM
help with sed to remove all text except for some Benanzo Linux - Software 7 01-04-2007 06:21 AM
how to remove path using sed dtcs Programming 2 12-25-2006 04:29 PM
sed and newline problem Swift&Smart Slackware 3 08-15-2006 06:32 AM
SED newline usage question -- I think sadarax Linux - General 3 02-18-2006 11:50 PM


All times are GMT -5. The time now is 01:14 PM.

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