Condition based concatenation.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Condition based concatenation.
# 1  
Old 09-16-2010
Question Condition based concatenation.

Hello,

I am looking for concatenating the lines based on conditions. Below are the contents of the file:

Infile:
-----
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
Test2.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
Test1.ProdtAtPlnt COLUMN (PARTITION
,
PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
Test3.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

CONDITION:
----------
If a line does not start with either Test1 OR Test2 OR Test3 then concatenate that line to previuos line else do nothing. The desired output should be (changed lines marked with *)

OUTPUT:
--------
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
*Test2.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
*Test1.ProdtAtPlnt COLUMN (PARTITION,PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
*Test3.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.

Thank You in advance.
# 2  
Old 09-16-2010
Try...
Code:
awk '/^Test[123]/&&NR>1{printf ORS}{printf $0}END{printf ORS}' file1 > file2

# 3  
Old 09-16-2010
try below perl

Code:
my $tmp="";
while(<DATA>){
  chomp;
  if(/^Test[123]/){
   if($tmp ne ""){
     print $tmp,"\n";
     $tmp=$_;
   }
   else{
    $tmp=$_;
   }
  }
  else{
   $tmp=$tmp.$_;
  }
  $flag=0 if ! /^Test[123]/;
}
print $tmp if $flag != 1;
__DATA__
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
Test2.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
Test1.ProdtAtPlnt COLUMN (PARTITION
,
PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
Test3.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

# 4  
Old 09-17-2010
Hi Ygor,

Thanks for the code.

This code worked for me perfectly for the scenario I gave. I may have scenario where in I would not have Test in common like

Test1.PO_Itm COLUMN ACTVIND.
DB.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

So now the condition will be
If a line does not start with either Test1 OR DB then concatenate that line to previuos line else do nothing. The desired output should be (changed lines marked with *)

Test1.PO_Itm COLUMN ACTVIND.
DB.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.

Also could you please explain the code for me.

Thanks.


Hi Summer cherry,

Thanks for your time in putting the code for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add new column based in condition

At begining of column 2 the same block (2000) have 3 lines, in the next block (2336) it have 9 lines and for block (2524) 3 lines. ... (0 Replies)
Discussion started by: jiam912
0 Replies

2. Shell Programming and Scripting

Concatenation lines based on first field of the lines

Hello All, This is to request some assistance on the issue that I encountered until recently. Problem is: I have a pipe delimited file in which some lines/records are broken. Now, I have to join/concatenate broken lines in the file to form actual record to make sure that the count of records... (8 Replies)
Discussion started by: svks1985
8 Replies

3. UNIX for Advanced & Expert Users

Concatenation of multiple files based on file pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

4. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

5. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

6. UNIX for Dummies Questions & Answers

inserting characters based on condition

hi i have a file that contains the data like this 12345 12453 8990998987 0989876656 12345678 12344 133678999 12345677 i should insert "+" and "-" signs for each line i,e., + 12345 - 12453 + 8990998987 - 0989876656 + 12345678 - 12344 (2 Replies)
Discussion started by: anurupa777
2 Replies

7. Shell Programming and Scripting

Arithmetic (number-based) if condition

Hi Folks I'm looking for help with if statement. I'm reading the file with header (starts with 0 on position 1 in the line) and data (starts with 1 on position 1 in the line). I have to check if the number from header (should be number of data rows) equal actual count of the data rows. ... (4 Replies)
Discussion started by: viallos
4 Replies

8. Shell Programming and Scripting

Comment based on a condition

I want to comment 2 lines based on a condition. If THEN occurs immediately after WHEN then i have to comment both the lunes For example : $cat file1.txt CASE WHEN THEN 1 WHEN c1= 'I' AND c2= '0' THEN 2 So in this example i want to... (2 Replies)
Discussion started by: ashwin3086
2 Replies

9. Shell Programming and Scripting

Merging of all files based on a condition

Hi Friends, I am new to UNIX. I need to merge all the files(to FINAL.txt) in single directory based one condition. Out of all the files one of file will have specific value like :GF01: at any where in the file. so the file which is having :GF01: should be appended at the last. EX:... (5 Replies)
Discussion started by: arund_01
5 Replies

10. Shell Programming and Scripting

transpose based on condition

Hi, I have the oracle table coulns in an order like date, state1, state2....state9 and i need to prepare data from the script output for loading in to this table The script is #!/bin/ksh /usr/xpg4/bin/awk -F"-" '{print $2,$4}' /aemu/ErrorLogs/data/MissingCGIcount.txt |... (5 Replies)
Discussion started by: aemunathan
5 Replies
Login or Register to Ask a Question