The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join two files koti_rama Shell Programming and Scripting 5 08-05-2008 01:20 AM
Join jazz8146 UNIX for Dummies Questions & Answers 5 01-29-2008 07:42 AM
join (pls help on join command) summer_cherry Shell Programming and Scripting 1 12-31-2007 01:19 AM
Join help with -e option Ken555 UNIX for Dummies Questions & Answers 2 08-21-2006 07:50 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2008
Registered User
 

Join Date: Aug 2007
Posts: 46
Stumble this Post!
awk, join or sed

Code:
$ cat file1
a:23:43
A
B
C
a:24:21
a:23:44
S
D
A
F
a:24:44
a:23:45
S
D
E
a:24:45

$ cat file2
a:23:53
T
B
E
W
a:24:53
a:23:54
S
W
F
a:24:54
a:23:56
Q
W
W
E
E
a:24:56
How can i join both the files such that one instance of file1 (from the line starting with a to the next line starting with a) then one instacne of file2(similar)..
The starting line "a" is always constant.

i.e. required output:

Code:
a:23:43
A
B
C
a:24:21
a:23:53
T
B
E
W
a:24:53
a:23:44
S
D
A
F
a:24:44
a:23:54
S
W
F
a:24:54
a:23:45
S
D
E
a:24:45
a:23:56
Q
W
W
E
E
a:24:56
I tried normal join, but no luck.

Please.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-15-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
Try and adapt this awk program :
Code:
awk '
FNR==1 { occ=1 ; file++ ; blk="" ; cnt=0 }
{
   blk = blk (blk ? ORS : "") $0
}
/^a:/ {
   occ = 1-occ
   if (occ) {
      rec[++cnt, file] = blk
      if (maxr<cnt) maxr = cnt
      blk=""
      next
   }
}
END {
   for (r=1; r<=maxr; r++) {
      for (f=1; f<=file; f++) {
         print rec[r, f]
      }
   }
}
    ' file1 file2
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0