Joining multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining multiple lines
# 1  
Old 02-23-2005
Power Joining multiple lines

I have data of the following form. It starts its first line with a '{' and ends the last line with a '}'. What I need to do is to join every line starting with the '{' and ending with the '} into one line in the output file. If it was just this one block, I could do a 13 joins in vi, however it contains thousands of blocks of data. Any help would be greatly appreciated.

{F,10,A,R,E,0600,0400,"XXXX"|
T,01, 13,V, 0, 45,00,1,01,01,B,L,0,0 |
T,02, 14,V, 15, 45,00,1,01,01,B,L,0,0 |
T,03, 40,V, 59, 15,00,1,02,01,B,L,0,0 |
T,04, 30,V, 102, 15,00,1,01,01,B,L,0,0 |
T,05, 35,V, 128, 15,00,1,01,01,B,L,0,0 |
T,06, 10,V, 163, 50,00,1,01,01,B,L,0,0 |
T,07, 11,V, 203, 45,00,1,02,02,B,L,0,0 |
T,08, 4,V, 400, 175,00,1,01,01,B,L,0,0 |
T,09, 44,V, 420, 0,00,1,01,01,B,L,0,0 |
T,10, 31,V, 460, 15,00,1,02,01,B,L,0,0 |
T,11, 29,V, 485, 15,00,1,02,01,B,L,0,0 |
T,12, 36,V, 510, 15,00,1,02,01,B,L,0,0 |
T,13, 21,V, 550, 130,00,1,02,01,B,L,0,0 |
}
# 2  
Old 02-23-2005
something to start with - assuming no embedded "blocks":

nawk -f sw.awk file

sw.awk:
Code:
substr($0,1,1) == "{" && substr($0,length($0),1) == "|" {
   start=1;
}
substr($0,1,1) == "}" && start{ print; start=0;next}
start {printf $0;next}
1

# 3  
Old 02-23-2005
And here's a sed solution that should work with most newer seds....

Code:
#!/bin/sed -nf

H
/}/ {
  x
  s/\n//g
  p
}

Invoke with
./foo.sed my_file

Cheers
ZB
# 4  
Old 03-01-2005
Quote:
Originally Posted by zazzybob
And here's a sed solution that should work with most newer seds....

Code:
#!/bin/sed -nf

H
/}/ {
  x
  s/\n//g
  p
}

Invoke with
./foo.sed my_file

Cheers
ZB
You solution worked great. Thanks very much.
# 5  
Old 03-02-2005
We can do with awk as,

Code:
awk '/^{/,/^}/ { printf $0 }END { print "\n"}' <filename>

HTH.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

2. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

3. Shell Programming and Scripting

Joining lines in different way

Hi all, I'm excited to the part of unix.com forum, and noob to it. I have an query, where I have an file and it contains data like this use thread when posting do no I was expecting the result as use thread thread when when posting posting do do no use thread when thread when... (6 Replies)
Discussion started by: Jose Nirmal
6 Replies

4. Shell Programming and Scripting

Joining contents in multiple lines to a single line

I do have a file with contents splited into multiple lines ADSLHLJASHGLJSKAGHJJGAJSLGAHLSGHSAKBV AJHALHALHGLAGLHGBJVFBJVLFDHADAH GFJAGJAGAJFGAKGAKGFAK AJHFAGAKAGAGKAKAKGKAGFGJDGDJJDGJDJDFAG ... ... .... 100's of lines I would like to rearrange the content of this file so it will be a... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

5. UNIX for Dummies Questions & Answers

JOINING MULTIPLE LINES IN A TEXT FILE USING GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. I WANT THE DATA... (0 Replies)
Discussion started by: KANNI786
0 Replies

6. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

7. Shell Programming and Scripting

Need help joining lines

Hi All, I need the command to join 2 lines into one. I found lots of threads but none give me the sollution. Probably because unix scripting is one of my best features ;) I got a logfile where line 2 needs to be joined with line 1, lines 4 needs to be joined with line 3 etc If you need... (16 Replies)
Discussion started by: rene21976
16 Replies

8. Shell Programming and Scripting

joining two lines

Hi , I want to join two lines in a file, where the second line contain query string. if it doesn't contain that string i don't want to join e.g. Input file is as following: name fame game none none none name fame game cat eat mice I need output file as name fame game none none... (2 Replies)
Discussion started by: ashrafonics
2 Replies

9. Shell Programming and Scripting

Joining lines from two files - please help

Hello, I have 2 files say File 1 has ABC DEF GHI File 2 has 123 456 789 I need output as ABC 123 DEF 456 GHI 789 I tried awk and sed but not able to get it in the right way. :confused: Please help. Thanks (25 Replies)
Discussion started by: chandra004
25 Replies

10. Shell Programming and Scripting

Joining 2 lines in a file together

Hi guys, I've got a log file which has entries that look like this: ------------------------------------------------------------------------------- 06/08/04 07:57:57 AMQ9002: Channel program started. EXPLANATION: Channel program 'INSCCPQ1.HSMTSPQ1' started. ACTION: None. ... (3 Replies)
Discussion started by: m223464
3 Replies
Login or Register to Ask a Question