Concatenating the lines with different pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating the lines with different pattern
# 1  
Old 11-23-2009
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 original file and put the result into another file File2.

grep -E "^SH1|^RD1" Original_File1 > File2 ( which has the data as shown below)

cat File2:
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

Then I combined the two records (SH1 AND RD1) using sed command and put it into File3.

sed 'N;s/\n/,/' File2 > File3

cat File3:
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

New Condition:
I am trying to do is: if the SH1 record is missing the corresponding RD1 record below it then append 8 (commas) after the end SH1 record else append the RD1 record(if existis). Also, vice versa...if RD1 record is missing the SH1 record above it, then prepend the record with 4 (commas).

All I trying to do is to create a file without combining SH1 record with SH1 record or RD1 record with RD1 record. Can we perform a loop to check ths.

If the record is something like this

SH1,QC,12334
RD1,MO,898909,35476 --- let say if this record is missing then in that case I want to add 6 commas in the above record.
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878 ---let say this record is missing
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978

Output:
SH1,QC,12334,,,,,,,
SH1,BC,34556,RD1,FG,2341212,909090
,,,,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978


I will really appreciate your advice on this.

Thanks
# 2  
Old 11-23-2009
Given these conditions, I think your data should have a record separator (a "pair-separator" if you will). If you have this, for example:

Code:
SH1,QC,12334
RD1,MO,898909,35476

then it could mean -
(a) the RD1 record corresponds to the SH1 record, or
(b) the SH1 record is missing its corresponding RD1 record and the RD1 record is missing its corresponding SH1 record as well.

And each of these assumptions yield different outputs.

This ambiguity could've been removed had there been a record separator/pair-separator.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Concatenating the lines of a data

I have a data of 1 lac lines with the following format 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 .... .... (6 Replies)
Discussion started by: aravindj80
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 and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

8. 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

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. 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