Concatenating the lines of a data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating the lines of a data
# 1  
Old 02-01-2012
Concatenating the lines of a data

I have a data of 1 lac lines with the following format

Code:
abcde,1,2,3,4,
,ee
,ff
,gg
,hh
,mm
abcde,3,4,5,6,
,we
,qw
,as
,zx
,cf
abcde,1,5,6,7,
,dd
,aa
,er
....
....
....
abcde,1,5,4,3,
,kk
,gh

abcde is there in all record , it is starting word and below lines are sub records

I would like to concatenate to make the output like

Code:
abcde,1,2,3,4,ee,ff,gg,hh,mm
abcde,3,4,5,6,we,qw,as,zx,cf
.
.
.
.
abcde,1,5,4,3,kk,gh

Please help with a script either in Unix Shell or Perl

Last edited by methyl; 02-01-2012 at 02:13 PM.. Reason: pelase use code tags
# 2  
Old 02-01-2012
Quote:
abcde,1,2,3,4,
Is the trailing comma on the "abcde" lines always present? It seems inconsistent with the structure of the rest of the data file.
# 3  
Old 02-01-2012
Hi aravindj80,

One solution using perl:
Code:
$ cat infile
abcde,1,2,3,4,
,ee
,ff
,gg
,hh
,mm
abcde,3,4,5,6,
,we
,qw
,as
,zx
,cf
abcde,1,5,6,7,
,dd
,aa
,er
....
....
....
abcde,1,5,4,3,
,kk
,gh
$ perl -pe 'chomp; print qq[\n] if $. > 1 && m/\A(?i:abcde)/ ; print qq[\n] if eof' infile
abcde,1,2,3,4,,ee,ff,gg,hh,mm
abcde,3,4,5,6,,we,qw,as,zx,cf
abcde,1,5,6,7,,dd,aa,er............
abcde,1,5,4,3,,kk

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 4  
Old 02-01-2012
Quote:
Originally Posted by aravindj80
I have a data of 1 lac lines with the following format

Code:
abcde,1,2,3,4,
,ee
,ff
,gg
,hh
,mm
abcde,3,4,5,6,
,we
,qw
,as
,zx
,cf
abcde,1,5,6,7,
,dd
,aa
,er
....
....
....
abcde,1,5,4,3,
,kk
,gh

abcde is there in all record , it is starting word and below lines are sub records

I would like to concatenate to make the output like

Code:
abcde,1,2,3,4,ee,ff,gg,hh,mm
abcde,3,4,5,6,we,qw,as,zx,cf
.
.
.
.
abcde,1,5,4,3,kk,gh

Please help with a script either in Unix Shell or Perl
awk solution
Code:
# awk '{a[x++]=$0}END{for(i=0;i<=x;i++)if(a[i]!~/abcde/)printf "%s",a[i];else{if(i!=0)printf "\n%s",a[i];else{printf "%s",a[i]}}}' file
abcde,1,2,3,4,,ee,ff,gg,hh,mm
abcde,3,4,5,6,,we,qw,as,zx,cf
abcde,1,5,6,7,,dd,aa,er............
abcde,1,5,4,3,,kk,gh

regards
ygemici
# 5  
Old 02-01-2012
Hmm. I asked about the trailing comma because the sample output in post #1 does not contain a double-comma.
# 6  
Old 02-01-2012
Nice touch, methyl. I read your previous message but didn't realize. I will change the script later because it seems wrong as is.

Regards,
Birei
# 7  
Old 02-02-2012
Birei You are great and Superb , it worked very fast and can you help me for this also on perl

https://www.unix.com/shell-programmin...hing-perl.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problems concatenating data using UNIX?

Hello, Can somebody help me to solve this inconsistent data issue. I have a pipe delimiter file and one of the column is a comment. I am trying to concatenate into one single sentence. For instance, I have a file actually with 2 records but the way it considers the first record is as different... (3 Replies)
Discussion started by: brialp
3 Replies

2. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

3. Shell Programming and Scripting

Concatenating more than two lines into one based on some columns

Hi, I need to concatenate some lines in a file based on the First 4 coloumns of a file .. (For Eg.) Consider a file ... I,01,000002,0666,00000.00,000,00,000,000, ,0 I,01,000002,0667,00000.00,000,00,000,000, ,0 I,01,000002,0666,00056.10 I,01,000002,0667,00056.10 I,01,000002,0666,00001... (6 Replies)
Discussion started by: Sri3001
6 Replies

4. Shell Programming and Scripting

Concatenating lines ending with '+' using awk

Hi, I have an ASCII text file where some of the lines are ending with '+' character. I have to concatenate the next successive line with those lines having the trailing '+' char by removing that char. The below awk code has some problems to do this task: awk '{while(sub(/\+$/,"")) {... (12 Replies)
Discussion started by: royalibrahim
12 Replies

5. Shell Programming and Scripting

Concatenating lines in bash

Hi, I'm attempting to join two lines in a file which are separated by a line break. The file contents are shown below: event_id=0 id=0_20100505210853 IFOconfig=HLV template=TaylorF2 Nlive=1000.0 Nruns=1.0 NIFO=3... (7 Replies)
Discussion started by: Supersymmetric
7 Replies

6. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

7. Shell Programming and Scripting

Concatenating the lines with different pattern

Hi, I have put a similar question in one of the other threads through which I got the solution shown below but I have some more condition to add to it, hence have further queries on it. I appologies if I should be putting this with the old thread. I have a file which perform a grep on the... (1 Reply)
Discussion started by: simi28
1 Replies

8. Shell Programming and Scripting

concatenating selected lines of multiple files

Hi, I would like a shell script that reads all files in a directory and concatenate them. It is not a simple concatenation. The first few lines of the files should not be included. The lines to be included are the lines from where 'START HERE' appears up to the end of the file. For example, I... (4 Replies)
Discussion started by: laiko
4 Replies

9. Shell Programming and Scripting

Concatenating the two lines in a file

hi My requirement is i have a file with some records like this file name ::xyz a=1 b=100,200 ,300,400 ,500,600 c=700,800 d=900 i want to change my file a=1 b=100,200,300,400 c=700,800 d=900 if record starts with " , " that line should fallows the previous line.please give... (6 Replies)
Discussion started by: srivsn
6 Replies

10. Shell Programming and Scripting

Concatenating lines and formatting.

Hi, I have a html file which is unformatted and need to concatenate the lines between each "table" statement in order to run an awk statement on it. Here is the example of the raw file: <table border="0" cellspacing="0" cellpadding="0" class="playerDetails"> def ... (3 Replies)
Discussion started by: Tonka52
3 Replies
Login or Register to Ask a Question