The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to join lines glamo_2312 UNIX for Dummies Questions & Answers 8 09-16-2008 05:17 AM
Join Every 5 Lines With $ Symbol ntgobinath Shell Programming and Scripting 3 05-08-2008 11:01 AM
to join the corresponding lines using shell commands or awk cdfd123 Shell Programming and Scripting 7 12-10-2007 04:45 PM
Awk Join multiple lines hitmansilentass Shell Programming and Scripting 5 10-27-2006 12:04 PM
join two lines together tine Shell Programming and Scripting 4 12-12-2003 12:34 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-23-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
How can i join three lines into one in unix?

Hi all,

I am trying to merge three lines into one in my unix text file.

My text file sis omething like this.
Code:
xxxxxxxxx
yyyyyyyyyyy
zzz
aaaaaaaaa
bbbbbb
ccccc
Expected out put is
Code:
xxxxxxxxx yyyyyyyyyyy zzz
aaaaaaaaa bbbbbb ccccc
I tried with awk as shown below.
Code:
awk '{printf $0 " "; getline; print $0}'
But it is working only for merging two lines. Please help me in modifying this awk or any other alternative for this?
  #2 (permalink)  
Old 07-23-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,872
HTH
Code:
$ cat file.txt
xxxxxxxxx
yyyyyyyyyyy
zzz
aaaaaaaaa
bbbbbb
ccccc
$ perl -pi -e 's/\n/ / if $.%3' file.txt
$ cat file.txt
xxxxxxxxx yyyyyyyyyyy zzz
aaaaaaaaa bbbbbb ccccc
  #3 (permalink)  
Old 07-23-2009
lathavim lathavim is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 73
See if this works.

Code:
awk '{
if (NR % 3)
printf("%s ", $0)
else
printf("%s\n", $0)
}' file
  #4 (permalink)  
Old 10-06-2009
Kibou Kibou is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid, Spain
Posts: 9
Quote:
Originally Posted by lathavim View Post
See if this works.

Code:
awk '{
if (NR % 3)
printf("%s ", $0)
else
printf("%s\n", $0)
}' file
Hi, I was reading this.. this work perfect, but I need something slightly different. I need print in the same line two lines if it finds a pattern ( a string in the first line). I am trying different things with awk.. but cant get it to work

It would be something like

select bla bla
update bla bla
bla bla
insert into bla bla

and the resulting file must find "update" and then print that line and the next one altogether, like:

select bla bla
update bla bla bla bla
insert into

Any help would be great.....
  #5 (permalink)  
Old 10-06-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
Try this for printing the line with pattern, line above and the next line.
awk '/update/{where=NR;print}NR==where+1 && where!=0 {print}'
  #6 (permalink)  
Old 07-23-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
Thanks lathavim..it worked perfectly..Pludi, i am looking for a shell command..anyway thanks for your reply.
  #7 (permalink)  
Old 07-23-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,872
You do know that Perl is as much a "shell command" as awk (both aren't shell built-ins but external commands), and both commands given do the same thing.
Reply

Bookmarks

Tags
merge, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0