LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-01-2012, 04:38 PM   #1
medirecpr
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Rep: Reputation: Disabled
Bash command to turn multiple lines to one line


I am trying to modify a file which appears to be multiple lines, but when I check in VIM it does not have any "^M" at the end of each line.

I opened the file in Word, and it shows a "" at the end of each line.

I am able to add a "~" at the end of each line using:

Code:
sed -i -e 's/\r/\~/g'
All I need now is to have all the lines concatenate into a long line, by modifying the file, not creating a new one.

any thoughts? thanks,

Last edited by medirecpr; 05-01-2012 at 04:39 PM. Reason: adding details
 
Old 05-01-2012, 04:42 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
FYI - "^M" is how VI (and many other linux readers) interprets the DOS OEL terminators. These only appear in DOS-formatted ASCII. Most linux distributions include a "dos2unix" utility which can be used to convert these DOS terminators to UNIX terminators, which will cause the "^M"'s to disappear from the file when viewing with VI.

I know this doesn't answer your question, I'm just providing some clarification.
 
Old 05-01-2012, 06:42 PM   #3
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
hi,

Code:
tr -d '\n' <urFile
Code:
sed ':a;N;s/\n//;ta'urFile
`t' is a kind of GOTO, `a' is just a mark, `:a' is where to go.

Last edited by everToulouse; 05-01-2012 at 06:54 PM.
 
Old 05-01-2012, 09:52 PM   #4
medirecpr
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
It does

Quote:
Originally Posted by suicidaleggroll View Post
FYI - "^M" is how VI (and many other linux readers) interprets the DOS OEL terminators. These only appear in DOS-formatted ASCII. Most linux distributions include a "dos2unix" utility which can be used to convert these DOS terminators to UNIX terminators, which will cause the "^M"'s to disappear from the file when viewing with VI.

I know this doesn't answer your question, I'm just providing some clarification.
It does, thank you!
 
Old 05-02-2012, 01:55 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Assuming you are correct about only \r:
Code:
awk 'ORS=" "' RS="\r" file
 
Old 05-02-2012, 07:39 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
BTW, sed by default only operates on single lines at a time. It takes a line into it's pattern buffer, minus the newline, applies the given expressions to it, then re-adds the newline when printing out the results. So a simple "s///" on its own can't ever affect multiple lines.

To do so requires telling sed to store multiple lines into the buffer first, usually with the "N" command. Then the substitution pattern can target the newline between them. everToulouse showed you how it's done.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


tr is generally the better option, though. Notice, however, that just deleting the newline characters means the lines get concatenated without any spaces between them. So this might be closer to what you expect:

Code:
tr -s '\n' ' ' <file
The -s option "squeezes" multiple newlines into a single space, so any blank lines will be removed. Remove it if not needed.

Last edited by David the H.; 05-02-2012 at 07:40 AM. Reason: minor rewording
 
  


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
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 07:09 AM
Multiple lines at the command line - how?! jsp_1983 Linux - Newbie 4 02-25-2011 09:04 PM
have a Variable with multiple lines of data need to have it all on one line seperated xskycamefalling Programming 11 05-12-2009 05:44 AM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 12:43 PM
How to turn off Xserver in the gui and turn it off in the command line geminiviper Linux - Newbie 8 08-20-2004 09:05 AM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration