Concatenating two line into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating two line into one
# 1  
Old 11-16-2009
Concatenating two line into one

Hi,

I have a scenario where I have a data file something like this below:


SH1,QC,12334
RD1,MO,898909,35476
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978

All I have to do is to concatenate the record starting with SH1 with the record starting with RD1 which is just below it( and NOT with the record starting with RD1 which few lines below). I am trying to get the output something like below:


SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978


Please advice on this.

Thanks
# 2  
Old 11-16-2009
This command joins every two lines and inserts a comma:
Code:
sed 'N;s/\n/,/' infile

awk:
Code:
awk '/^SH1/{ORS=","}/^RD1/{ORS=RS}1' infile


Last edited by Scrutinizer; 11-16-2009 at 07:44 PM..
# 3  
Old 11-16-2009
Code:
$
$ cat f7
SH1,QC,12334
RD1,MO,898909,35476
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978
$
$ ## using Perl
$ perl -lne 'BEGIN{undef $/} s/\n(RD1)/,$1/g; print' f7
SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978

$
$ ## using awk
$ awk '/^SH1/{printf "%s,", $0} /^RD1/' f7
SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978
$
$

tyler_durden
# 4  
Old 11-16-2009
Concatenation the lines.....

Thanks a bunch for the suggestion. All the three suggestion worked for me.
I am thinking of using : sed 'N;s/\n/,/' infile.

I little new to unix. Would you be able to explain how this is working.
sed 'N;s/\n/,/' infile

Thanks again

Quote:
Originally Posted by Scrutinizer
This command joins every two lines and inserts a comma:
Code:
sed 'N;s/\n/,/' infile

Quote:
Originally Posted by durden_tyler
Code:
$
$ cat f7
SH1,QC,12334
RD1,MO,898909,35476
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978
$
$ ## using Perl
$ perl -lne 'BEGIN{undef $/} s/\n(RD1)/,$1/g; print' f7
SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978
 
$
$ ## using awk
$ awk '/^SH1/{printf "%s,", $0} /^RD1/' f7
SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978
$
$

tyler_durden
# 5  
Old 11-16-2009
Code:
awk '{printf "%s,",(/^SH1/)?"\n"$0:$0}'  urfile
cat urfile |xargs -n2

# 6  
Old 11-16-2009
Quote:
Originally Posted by simi28
Would you be able to explain how this is working.
sed 'N;s/\n/,/' infile
Sure, the first sed command is
Code:
N

which joins two lines together, the second is

Code:
s/\n/,/

which replaces a newline character with a comma.

Last edited by Scrutinizer; 11-16-2009 at 08:05 PM..
# 7  
Old 11-16-2009
Extracting and then Concatenating the lines

Thanks a bunch for your help and advice.
I am really sorry, I should have put the question below with my original question. Is there a way to grep on multiple pattern.
My original file is something like below. I will have to create another file with all the SH1 and RD1 records and then concatenate them.Concatenated file is my final file.


FH1,ONVW1,Inc.,29383,2.20,20091104,125233,,Y
SH1,QC,20091031,359680,261715151
RD1,RG,09-0681785-0014,,20091102,101900,20150427,,
BD1,4577
SP1,1,
SF1,497499150
SH1,QC,20091103,359936,261784930
RD1,RG,09-0685249-0077,,20091103,123200,20141028,,
BD1,4577
SP1,1,


Thanks

Quote:
Originally Posted by Scrutinizer
The first sed command is
Code:
N

which joins two lines together, the second is

Code:
s/\n/,/

which replaces a newline character with a comma.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating Output

Hello all The following line : df -h | awk '{print $5}'| head -2 |tail -1 gives me an output of '2.2G' How can I remove the 'G' so that I can use the 2.2 for further calculations ? (8 Replies)
Discussion started by: Junaid Subhani
8 Replies

2. UNIX for Dummies Questions & Answers

Concatenating columns

Hi I have the following input file, It is a tab delimited file ISOCOUNTRYCODE POSTALCODE CITY HNO STREETBASENAME STREETTYPE FIN 40950 Muurame Teollisuus tie FIN 02160 Westendintie FIN 33210 Tampere Päämäärän kuja... (2 Replies)
Discussion started by: ramky79
2 Replies

3. UNIX for Dummies Questions & Answers

Concatenating

Hi, I have file called "3rdparty.dat" I want to concatenate current YYYYMMDD to it. Snd result should be like 3rdParty20111110.dat. How can i do this? Thanks in advance. (3 Replies)
Discussion started by: raj.shah.0609
3 Replies

4. Shell Programming and Scripting

de concatenating a string

I have a variable var=string1:string2:string3 I want to get the string de-concatenated and put it as var1=string1 var2=string2 var3=string3 Thanks in advance. ---------- Post updated at 02:18 PM ---------- Previous update was at 01:45 PM ---------- I got the solution as below:... (2 Replies)
Discussion started by: Deepak62828r
2 Replies

5. Shell Programming and Scripting

need help in concatenating

Hi All , i`m writing a script , i stucked in middle . Script echo "Please Enter the INSTANCE name" read iName echo "The INSTANCE name is $iName" more /opt/IBMIHS*/conf/httpd.conf_"$iName" script end here i`m getting error as : Error /opt/IBMIHS*/conf/httpd.conf_w101:... (7 Replies)
Discussion started by: radha254
7 Replies

6. Shell Programming and Scripting

CSH: Concatenating Strings, how to add new line character and functions?

Hello, I'm trying to run a program on a directory (traverse sub dirs too) through my csh script. Arrays support in CSH is appalling, something like associative arrays would have helped me do this so much easier. Anyway, I want to hold some details extracted from the program and then at the... (0 Replies)
Discussion started by: ragabonds
0 Replies

7. Shell Programming and Scripting

concatenating strings

I m new to shell scripting and what i want is take as an i/p from command line the name of the file and inside my script i should redirect the o/p of my few commands to this file concatenated with .txt for example if i give ./linux filename i should get the o/p in filename.txt i need to... (2 Replies)
Discussion started by: tulip
2 Replies

8. Shell Programming and Scripting

Concatenating Different # of Variables

Hi, I'm quite new at unix and was wondering if anyone could help me with this. I have 2 arrays: eg. STAT=online, STAT=offline, STAT=online WWN=xxxx1, WWN=xxxx2, WWN=xxxx3 I got these information from a script using fcinfo hba-port that runs through a loop. Now, I want to store... (2 Replies)
Discussion started by: jake_won
2 Replies

9. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies

10. UNIX for Dummies Questions & Answers

concatenating x files into a one...

... i have 4 files to concatenate but in a certain order and i wanted to do it in a shorter one line command , if possible ! 4 files : file , file0 , file1 and file2 file1 into file2 file0 into the result file into the result thanks in advance Christian (1 Reply)
Discussion started by: Nicol
1 Replies
Login or Register to Ask a Question