How can i join three lines into one in unix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i join three lines into one in unix?
# 8  
Old 10-06-2009
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}'
# 9  
Old 10-06-2009
Code:
nawk 'c&&!--c;/update/{c=1;printf $0 OFS}' myFile

# 10  
Old 10-06-2009
HI Rdhanek,

you can make use of this code:

Code:
paste -s -d "  \n" filename

output:
Code:
xxxxxxxxx yyyyyyyyyyy zzz
aaaaaaaaa bbbbbb ccccc

Regards,
Sanjay
# 11  
Old 10-07-2009
Quote:
Originally Posted by protocomm
try this...

Code:
awk '{if (NR%3) {ORS="";print " "$0} else {ORS="\n";print " "$0}}'
 file

Thank you very much for your quick response. It does not quite yet what I am looking for.. I need only two lines, for what I just changed "NR%2" like this:

Code:
awk '{if (NR%2) {ORS="";print " "$0} else {ORS="\n";print " "$0}}'
 file

The problem is, that I want to print like this only when it meets a patter, for example:

input:

select <something><something>
update <something><something>
<something><something>
insert into <something><something>

output:

select <something><something>
update <something><something><something><something>
insert into <something><something>

I just cant figure out how to do it.. I dont know if I have to use something like

Code:
/update/{ if (NR%2) ..

or

Code:
if((NR%2) && ($1=="nueva"))

both just doesnt work...

---------- Post updated at 11:08 AM ---------- Previous update was at 11:01 AM ----------

Quote:
Originally Posted by vgersh99
Code:
nawk 'c&&!--c;/update/{c=1;printf $0 OFS}' myFile

Thanks, this works great, but just print the two lines I want in one, that's perfect Smilie but it doesnt print the rest of the file. How to make this work then?
# 12  
Old 10-07-2009
Code:
nawk 'c&&!--c{print;next};/update/{c=1;printf $0 OFS;next}1' myFile

# 13  
Old 10-07-2009
if the total number of lines is in multiples of 3, then
Code:
sed -n 'N;N;s/\n/ /gp' FILENAME

# 14  
Old 10-07-2009
Quote:
Originally Posted by vgersh99
Code:
nawk 'c&&!--c;/update/{c=1;printf $0 OFS}' myFile

Congratulations, this works 100% perfect. Thanks so much. I will keep studying the uses of awk, which are quite extensive.
I really appreaciate your help, man.
Smilie

Regards.

---------- Post updated at 01:10 PM ---------- Previous update was at 11:56 AM ----------

Quote:
Originally Posted by Kibou
Congratulations, this works 100% perfect. Thanks so much. I will keep studying the uses of awk, which are quite extensive.
I really appreaciate your help, man.
Smilie

Regards.
Just one more question regarding your impressive advanced knowledge of awk:

I have appended my line when found the word "update", but I want this to happen only when next line doesnt contain either the string "alter table" or "insert into".

Like,

input:

Code:
select <something><something>
update <something><something>
<something><something>
insert into <something><something>
select <something><something>
update <something><something>
alter table <something><something>
update <something><something>
insert into <something><something>

output:

Code:
select <something><something>
update <something><something><something><something>
insert into <something><something>
select <something><something>
update <something><something>
alter table <something><something>
update <something><something>
insert into <something><something>

thank you so much.. I hope the explanation is clear enough now..

Last edited by Kibou; 10-07-2009 at 08:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join Lines

Hi how do I join files like below in script. Thanks, Ashan there are may line like this in the file. zone name DR_TMP_A_sev1_3eA vsan 200 pwwn 50:00:09:73:f0:16:35:08 pwwn c0:50:76:08:6e:dc:00:16 zone name DR_TMP_A_SVR2_3eA vsan 200 pwwn 50:00:09:73:f0:16:35:08 pwwn... (4 Replies)
Discussion started by: ashanabey
4 Replies

2. UNIX for Dummies Questions & Answers

how to join all lines in afile in unix

Hi, I have a unix file which has many lines, i need to join all the lines to single line. Eg: myfile.txt contains: a 123 45fg try and i need the output as : a 123 45fg try Please help me on this. Thanks! (2 Replies)
Discussion started by: RP09
2 Replies

3. Shell Programming and Scripting

Help in unix script to join similar lines of input

Hi, I have been thinking of how to script this but i have no clue at all.. Could someone please help me out or give me some idea on this? I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas. Let's say i have the following input. ... (3 Replies)
Discussion started by: rei125
3 Replies

4. Shell Programming and Scripting

join lines in file

I have a file like this: --------------------------------------------------------------- 26 00:04:48,440 --> 00:04:51,440 I don't know why he can't just do the Great Apache Flaming Arrow Act. 27 00:04:52,440 --> 00:04:54,839 Didn't you tell him to use the gopher snake? 28... (1 Reply)
Discussion started by: thailand
1 Replies

5. Shell Programming and Scripting

join two lines

I want to join this two lines but only when after him I have nothing or a comma Yes, I know Jonesy, and I'll give him about one more minute. this two lines must become Yes, I know Jonesy, and I'll give him about one more minute. thank you very much (11 Replies)
Discussion started by: thailand
11 Replies

6. Shell Programming and Scripting

join 2 lines

hi all i have sample and i need script to do this /dev/xxx oracle test /dev/sap 9999 000 88 99 i need the out put like this /dev/xxx oracle test /dev/sap 9999 000 88 99 can any one provide me with an idea to solve this problem (8 Replies)
Discussion started by: maxim42
8 Replies

7. Shell Programming and Scripting

Join the lines

Hi All, Currently, the output looks like this: hdisk0 queue_depth:3 hdisk1 queue_depth:3 hdisk2 queue_depth:1 hdisk3 queue_depth:1 I need to change the format to look like this: hdisk0 queue_depth:3 hdisk1 queue_depth:3 hdisk2 queue_depth:1 (8 Replies)
Discussion started by: Beginer0705
8 Replies

8. Shell Programming and Scripting

join lines

input1 x x input2 y x x z join input1 input2>>output ouput x x (2 Replies)
Discussion started by: repinementer
2 Replies

9. UNIX for Dummies Questions & Answers

how to join lines

can anyone tell me as "how to join all lines in a file " using a shell script Actually i have many files in a directory and for each file i want to join all the lines using a shell scrip . Thanks in advance!!! (8 Replies)
Discussion started by: glamo_2312
8 Replies

10. Shell Programming and Scripting

join two lines together

Hi, I have a file with on one line a uid, and on the next line a date. I am trying to make the to into one line. Here's an example: koppx 20031125 kraan 20031119 sarox 20031107 And this is what i want it to be: koppx;20031125 kraan;20031119 sarox;20031107 I have been trying... (4 Replies)
Discussion started by: tine
4 Replies
Login or Register to Ask a Question