The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 04:20 AM
Join jazz8146 UNIX for Dummies Questions & Answers 5 01-29-2008 11:42 AM
join (pls help on join command) summer_cherry Shell Programming and Scripting 1 12-31-2007 05:19 AM
Join help with -e option Ken555 UNIX for Dummies Questions & Answers 2 08-21-2006 10:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-15-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
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.
  #2 (permalink)  
Old 04-15-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0