Awk Multiline Record Combine?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Multiline Record Combine?
# 1  
Old 10-27-2008
Awk Multiline Record Combine?

I'm trying to use Awk to get the id and name fields ($1 and $2) of file1 combined with their corresponding multiline records in file2 that are separated by blank line. Both files are ordered so that the first line of file1 corresponds to the first set of multiline records in file2 and so on.

file1.txt
Code:
23315:MR FANOVER:BR:H:1994
45322:MR DEWEY:B:F:1996
23316:MR DREAMMAKER:BLK:X:1996
45323:MR MAN:BR:L:1997

file2.txt
Code:
99-01-14:BML:10:FT
99-01-31:BML:37:GD
99-06-18:MAY:73:FT
99-06-30:BML:71:SL

99-01-15:WDB:17:FT
99-01-25:MOHO:28:GD

99-07-2:BR:75:SY

99-12-6:NFLDO:32:SL
99-12-13:NFLD:39:FT
99-12-19:NFLD:40:FT

Desired Output:
Code:
23315:MR FANOVER:99-01-14:BML:10:FT
23315:MR FANOVER:99-01-31:BML:37:GD
23315:MR FANOVER:99-06-18:MAY:73:FT
23315:MR FANOVER:99-06-30:BML:71:SL

45322:MR DEWEY:99-01-15:WDB:17:FT
45322:MR DEWEY:99-01-25:MOHO:28:GD

23316:MR DREAMMAKER:99-07-2:BR:75:SY

45323:MR MAN:99-12-6:NFLDO:32:SL
45323:MR MAN:99-12-13:NFLDO:39:FT
45323:MR MAN:99-12-19:NFLDO:40:FT

Can anybody offer a solution?
# 2  
Old 10-27-2008
Use nawk or /usr/xpg47bin/awk on Solaris:

Code:
awk -F: 'NR == FNR { _[NR] = $1 FS $2; next }
!$1 ? ++c : $1 = _[c] FS $1
' OFS=: file1 c=1 file2

# 3  
Old 10-27-2008
Thanks radoulov that did the trick! You're a life-saver or maybe a database-saver in this case!
# 4  
Old 10-27-2008
Glad it worked!
Actually, it could be golfed Smilie

Code:
awk -F: '
  NR==FNR{_[NR]=$1FS$2;next}$1?$1=_[c]FS$1:++c
  ' OFS=: file1 c=1 file2

# 5  
Old 10-28-2008
perl:

Code:
$n=0;
open FH,"<b";
while(<FH>){
	$_=~tr/\n//d;
	if(length>0){
		if($arr[$n] eq ""){
			$arr[$n]=$_;
		}
		else{
			$arr[$n]=sprintf("%s!%s",$arr[$n],$_);
		}
	}
	else{
		$n++;
	}
}
close FH;
open FH,"<a";
while (<FH>){
	$temp=$arr[$.-1];
	@temp=split("!",$temp);
	@res=split(":",$_);
	for($i=0;$i<=$#temp;$i++){
		print "$res[0]:$res[1]:$temp[$i]\n";
	}
	print "\n";
}
close FH;

awk:
Code:
nawk -F":" 'BEGIN{c=1}
{
if(NR==FNR){
        _[NR]=$1FS$2
}
else{
        if(length($0)>0)
                print _[c]":"$0
        else
                c++
}
}
' a b


Last edited by summer_cherry; 10-28-2008 at 02:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine multiline to one line with proper format

Hello Guys, I have a file say FILE1.txt contains below data:- A B C D E F G H I J K L I need the output in another file as FILE2 as:- 'A', 'B', 'C', 'D', 'E', (7 Replies)
Discussion started by: jassi10781
7 Replies

2. Shell Programming and Scripting

Combine multiline to one line till a blank line

Hello, I have a file as :- ABC DEF GHI JKL <BlankLine> MNO PQR STU VWX <BlankLine> YZA I need it as below:- ABCDEFGHIJKL; MNOPQRSTUVWX; (3 Replies)
Discussion started by: jassi10781
3 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Shell Programming and Scripting

Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple. Input: Tue May 1 14:00 Header Record 1 is valid. Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15 Tue May 1 14:03 Header Record 1 is valid. Tue May 1 14:03 processing data... (4 Replies)
Discussion started by: Catullus
4 Replies

5. Shell Programming and Scripting

awk multiline matching

I have a file that looks something like this with lots of text before and after. Distance method: Sum of squared size difference (RST) </data> <pairwiseDifferenceMatrix time="02/08/11 at 13:08:27"> 1 2 1 448.82151 507.94231 2 ... (7 Replies)
Discussion started by: mgray
7 Replies

6. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

7. Shell Programming and Scripting

awk multiline with 1 or more variables (question)

am trying to grab the fields marked in red for monitoring purposes. each vfiler can have anywhere from 0-50 Path(s). in order to get here i run the following for filer in `cat filers.list` ; do ssh $filer vfiler status | awk '{print $1}' ; done this returns vfiler0 vfilert vfiler2 ... (2 Replies)
Discussion started by: riegersteve
2 Replies

8. Shell Programming and Scripting

Awk match a multiline pattern

Hello! i wanna match in a config file, one text with more than one lines, something like this: CACHE_SIZE{ 10000 M } I have problems with the ends of line, i think that i can match the end of the line with \n, but i can't get it Someone can help me with the regular expression? ... (18 Replies)
Discussion started by: claw82
18 Replies

9. Shell Programming and Scripting

Multiline read with multicharacter record seperator

I have a file like the below: Start <</NumCopies 0001>> 0223 098 et(5926)sh 0223 098 mt(5926)sh End Start <</NumCopies 0001>> 0224 098 et(5926)sh 0224 098 mt(5926)sh End This file needs to be split to seperate files. Each of the seperate file will need to... (3 Replies)
Discussion started by: pt14
3 Replies

10. Shell Programming and Scripting

transforming a multiline record to single line

Hi All I have a file like this <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdj vdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>b djvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> How will i change the file to ... (9 Replies)
Discussion started by: anju
9 Replies
Login or Register to Ask a Question