awk, join or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk, join or sed
# 1  
Old 04-15-2008
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  
Old 04-15-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

3. Shell Programming and Scripting

Join lines using sed or awk

Hi, I have text file that looks like this: blabla bla PATTERN LINE1 LINE2 bla bla bla PATTERN LINE1 LINE2 bla PATTERN LINE1 LINE2 bla (9 Replies)
Discussion started by: hench
9 Replies

4. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

5. Shell Programming and Scripting

Join two commands sed and grep

Hi all, I have two separate commands which I would like to join. Basically, I want to match a line and insert a character at the end of the previous line to the matched line Here is what I have gotgrep -B1 '^>' sed 's/$/*/' Any help is much appreciated thanks (5 Replies)
Discussion started by: kaav06
5 Replies

6. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

7. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

8. Shell Programming and Scripting

How to use SED to join multiple lines?

Hi guys, anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line? The end of each line will be replaced with a special char "#". I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Discussion started by: DrivesMeCrazy
12 Replies

9. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

10. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies
Login or Register to Ask a Question