Go Back   The UNIX and Linux Forums > Top Forums > Programming


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-16-2012
Registered User
 
Join Date: Apr 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL PERL:Combining multiple lines to single line

Hi All
I need a small help for the below format in making a small script in Perl or Shell.

I have a file in which a single line entries are broken into three line entries.
Eg:
I have a
pen and
notebook.

All i want is to capture in a single line in a separate file.
eg: I have a pen and notebook.

Thanks in Advance for your help.

Regards
Kalaiela
Sponsored Links
    #2  
Old 04-16-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
$ while read a; do echo -n "$a "; done < input.txt
I have a pen and notebook.

Sponsored Links
    #3  
Old 04-16-2012
Registered User
 
Join Date: Apr 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Kamraj

Could you pls explain me how your code works. Its not working in my machine.I got the output as below.

> while read a; do echo -n "$a "; done < input.txt
-n I have a
-n pen and
-n notebook.

More over my input file is like below.
I have a
pen and
notebook.


I have a
pen and
notebook.
Like this it is repeating with two blank lines in the middle.
what i want is
I have a pen and notebook.
I have a pen and notebook.

Thanks in Advance.
    #4  
Old 04-16-2012
Registered User
 
Join Date: Jun 2011
Location: From far
Posts: 987
Thanks: 21
Thanked 232 Times in 226 Posts

Code:
cat FILE
I have a
pen and
notebook.

I have a
pen and
notebook.
                                                                          ~
perl -00 -pe 's/\n/ /g; s/ $/\n/' FILE
I have a pen and notebook.
I have a pen and notebook.

Sponsored Links
    #5  
Old 04-17-2012
Registered User
 
Join Date: Jul 2011
Location: Pune, India
Posts: 54
Thanks: 9
Thanked 6 Times in 6 Posts

Code:
 
$ cat file
I have a
pen and
notebook.
 
$ perl -pe 's/\n/ /g;' file
I have a pen and notebook.

if you want output in another file, you can simplye redirect it


Code:
 
$ perl -pe 's/\n/ /g;' file > file1


Njoy!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM
Combine multiple lines in single line The One Shell Programming and Scripting 8 10-26-2010 12:15 PM
Multiple lines into a single line RickyC9999 Shell Programming and Scripting 4 02-22-2010 02:41 PM
Awk multiple lines with 3rd column onto a single line? SoMoney Shell Programming and Scripting 4 12-06-2008 07:59 AM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM



All times are GMT -4. The time now is 12:04 AM.