Multiple lines merged into one required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple lines merged into one required
# 1  
Old 05-31-2013
Multiple lines merged into one required

i have a file in the format
Code:
acti_id|signature
1|abc
def
xyz
2|lmn
pqr
lmn
3|ggg
ppp
mmm

it is in csv format
i want the file in the format
Code:
act_id|signature
1|abcdefxyz
2|lmnpqrlmn
3|gggpppmmm

i have tried awk but without much success. i replaced the new line with null but it combined all lines into one.

Code:
awk '{printf $0""}' file.csv

but this didnt work....
please help

Last edited by Scott; 05-31-2013 at 06:21 PM.. Reason: Code tags for data too
# 2  
Old 05-31-2013
Code:
skrynesaver@busybox ~/tmp$ cat tmp.dat
1|abc
def
xyz
2|lmn
pqr
lmn
3|ggg
ppp
mmm

skrynesaver@busybox ~/tmp$ perl -ne 'if(/\d+\|\w+/){print "$line\n";chomp($line=$_)}else{chomp($line.=$_)};END{print "$line\n"}' tmp.dat

1|abcdefxyz
2|lmnpqrlmn
3|gggpppmmm

# 3  
Old 05-31-2013
Could this help you ?
Code:
 awk '{printf $0~/\|/?"\n"$0:$0}' file.csv

# 4  
Old 05-31-2013
the perl one ain't working properly....its happening in the way
Code:
1|abcdefxyz2|lmnpqrlmn3|gggpppmmm

its happening with the awk form with just one exception...
there is blank in the first line and the cursor remains at the point after the last character...i need the cursor as a new line else my etl tool won't be able to read the last line.....
# 5  
Old 05-31-2013
Code:
awk '{printf ($0~/\|/ && NR >1)?"\n"$0:$0} END {printf "\n"}' file.csv

# 6  
Old 05-31-2013
thanks lot for your help...a little tweaking was required and it performed like a dream.....thanksssss
# 7  
Old 05-31-2013
With %s in printf it becomes immune against % and \ characters:
Code:
awk -F"|" '{printf (NF>1 && NR>1) ? "\n%s" : "%s", $0} END {printf "\n"}' file.csv

---------- Post updated at 09:19 AM ---------- Previous update was at 08:49 AM ----------

A solution with collecting a string:
Code:
awk -F"|" 'NF>1 && NR>1 {print s; s=""} {s=s $0} END {print s}' file.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

sed one liner to Delete blank lines - Help required

Hi, cat test.txt BlankLine BlankLine BlankLine BlankLine ello hi helo BlankLine BlankLine heylo BlankLine BlankLine BlankLine done BlankLine BlankLine BlankLine (1 Reply)
Discussion started by: TomG
1 Replies

3. Shell Programming and Scripting

Help required deleting specific lines from file

Hi, I have a file with 20 columns of data and hundreds of lines of the same format. Here is an example line. The data repeats underneath with the same format. 15 1 4 GLY - 1 65 LYSH 23 N - 24 H - 634 O 0.188 157.552 487 48.70I have been sorting this data by hand but I was wondering if I... (3 Replies)
Discussion started by: livbaddeley
3 Replies

4. AIX

Grepping before and after lines for required string

Hi All, I am new to AIX unix . i need to grep for a pattern and if pattern is found then i need 3 before the pattern line found and 3 lines after the pattern found. (11 Replies)
Discussion started by: coolvibh
11 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

7. Shell Programming and Scripting

How to extract the required lines?

Hi Techies, I got struck in middle of a problem. I have the following data whose format is: a 1 a 2 a 3 a 4 b 1 b 2 b 3 b 4 c 1 c 2 c 3 c 4 I wanted the output from this to be: a 3 a 4 (11 Replies)
Discussion started by: 14341
11 Replies

8. Shell Programming and Scripting

To extract the required lines from a file

I have a files in a directory in this format data data data ---BEGIN CERT----- data data data ---END CERT ----- Now, I want to extract the lines starting from --BEGIN CERT-- and write the contents till the end of file into a new file.How can I do this for all the files in the... (1 Reply)
Discussion started by: sureshg
1 Replies

9. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies

10. Shell Programming and Scripting

Help required regarding Extracting lines from a file

I have a file containing the following contents All of us, including Zippy, our dog All of us, including Zippy and Zippy All of us, including Zippy and Zippy and Zelda Testing All of us Zippy Now, i wanna grep and get the lines which has only one occurance of word Zippy and starting with... (1 Reply)
Discussion started by: google_ever
1 Replies
Login or Register to Ask a Question