paste broken pieces of a record into one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting paste broken pieces of a record into one line
# 1  
Old 08-27-2009
paste broken pieces of a record into one line

I am working with a file that has some of the records broken into several lines, I need paste the pieces back into one line. All records should start with numeric id, and presently all lines do start with id. The last field of the record is "telephone", so I need to make sure that each line starts with id and ends with telephone field.

To illustrate see below current file:

Code:
10 name ABC address 123 MAIN ST telefone 123-123-1234
11 name PQR 
 address 234 GREEN AV
 telefone 234-234-2345
12 name XYZ address 123 MAIN ST
 telefone 345-345-3456

I need to change it into this:

Code:
10 name ABC address 123 MAIN ST telefone 123-123-1234
11 name PQR  address 234 GREEN AV telefone 234-234-2345
12 name XYZ address 123 MAIN ST telefone 345-345-3456

Here is what I tried:

Code:
 
gawk '{
        s=s " " $0;
        if($0 ~ "telephone") {
                print s;
                s="";
        };
}
END{
        if(length(s) > 0) {
                print s;
        }
}' ${1}

but result is one long line

10 name ABC address 123 MAIN ST telefone 123-123-1234 11 name PQR address 234 GREEN AV telefone 234-234-2345 12 name XYZ address 123 MAIN ST telefone 345-345-3456

I can't figure out the problem with my code.
Any suggestions would be appreciated.
# 2  
Old 08-27-2009

Code:
 awk 'NR > 1 && /^[0-9]/ { print "" }
 {printf "%s ", $0}
  END { print "" }' "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Broken line issue

Hi, I have a major problem in my source files. I have around 10 source files(around 20 GB).In one of the source files I am getting the broken line issue.My source files needs to be like this Sr.No~Ref.No~Address~Acc.No 1~ABC345~No.2/110~456474 2~ABC786~4w/54~458695 ... (4 Replies)
Discussion started by: Nivas
4 Replies

2. Shell Programming and Scripting

Copy some line and paste it after some line in same file

Hi, I want to know how I can copy line 6th to 10th and paste it after 17th line in same file. Thanks, Biplab (19 Replies)
Discussion started by: Biplab
19 Replies

3. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

4. Shell Programming and Scripting

Remove newline character or join the broken record

Hi, I have a very huge file, around 1GB of data. I want to remove the newline characters in the file but not preceded by the original end delimiter {} sample data will look like this 1234567 abcd{} 1234sssss as67 abcd{} 12dsad3dad 4sdad567 abcdsadd{} this should look like this... (6 Replies)
Discussion started by: ratheeshjulk
6 Replies

5. Shell Programming and Scripting

Paste every other line?

Hi! I have two files that look like this: File1 A B C D E F ... File2 Z Y X W V U (4 Replies)
Discussion started by: Limmnes
4 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Shell Programming and Scripting

Line gets splitted into 99 char long pieces

Hello, I have a script like follows. It reads a file, and with every line, it calls an "adapter" program, which just puts the line into MQ. When I run this locally, it works fine. When I run this on our company's server, one line is split into several pieces (99 characters long) and "adapter"... (1 Reply)
Discussion started by: Adamm
1 Replies

9. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

10. UNIX for Dummies Questions & Answers

Search and paste a line..

Hi everyone, I've never used Unix ever in my life and I just got a summer job that requires me to manipulate Unix =( I'm currently encountering a problem... I need to search for Keyword in a text file, and if I find Keyword , I'll have to output the whole line that contains Keyword into a new... (4 Replies)
Discussion started by: SeanDM
4 Replies
Login or Register to Ask a Question