merging two files with one word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging two files with one word
# 1  
Old 12-16-2008
merging two files with one word

how to merge two files connecting one word which is the same in both file one:

omg omg omg
omg omg omg
00:00:23:03:04 stanga laba7123
00:03:24:03:04 jonas laba324

and in the other there is:

00:00:23:03:04 whatever goes here
00:03:24:03:04 and here

and the rezult should look like this:
00:00:23:03:04 stanga laba7123 whatever goes here
00:03:24:03:04 jonas laba324 and here

if there is no word connecting both of the files just ignore the line ant do not save in the file: in this example to ignore:
omg omg omg
omg omg omg
# 2  
Old 12-16-2008
use the Search button to look for similar threads - there've been multiple threads on a similar subject.
# 3  
Old 12-17-2008
Quote:
Originally Posted by vgersh99
use the Search button to look for similar threads - there've been multiple threads on a similar subject.
sorry, but i find a lot of exaples for joining two file in different way. I can't find one ignoring lines which doesn't have similarities between two files.
# 4  
Old 12-17-2008
nawk -f tada.awk file1 file2

tada.awk:
Code:
FNR==NR { 
          for(i=2; i<=NF; i++) f1[$1] = ($1 in f1) ? f1[$1] OFS $i : $i
          next
}

$1 in f1 {
   if (!($1 in f2))
      f2[$1]=f1[$1]
   for(i=2; i<=NF; i++) f2[$1] = f2[$1] OFS $i
}
END {
  for (i in f2)
    print i, f2[i]
}

# 5  
Old 12-17-2008
Quote:
Originally Posted by vgersh99
nawk -f tada.awk file1 file2

tada.awk:
Code:
FNR==NR { 
          for(i=2; i<=NF; i++) f1[$1] = ($1 in f1) ? f1[$1] OFS $i : $i
          next
}

$1 in f1 {
   if (!($1 in f2))
      f2[$1]=f1[$1]
   for(i=2; i<=NF; i++) f2[$1] = f2[$1] OFS $i
}
END {
  for (i in f2)
    print i, f2[i]
}

thanks it works just fine with that example, but then files a bigger it get's messy. Now i realize whats wrong, if there is same 00:00:23:03:04 several times. let's say the
first file is:
Code:
omg omg omg
omg omg omg
00:00:23:03:04 stanga laba7123 
00:03:24:03:04 jonas laba324 
omg omg omg
00:00:23:03:04 stanga laba7123
omg omg omg

second is:
Code:
00:00:23:03:04 whatever goes here
00:03:24:03:04 and here
00:00:23:03:04 and even here

output:

00:00:23:03:04 stanga laba7123 whatever goes here
00:03:24:03:04 jonas laba324 and here
00:00:23:03:04 stanga laba7123 and even here

it could realize that the same first word could be later in the first file.

now i get:

Code:
00:00:23:03:04 stanga laba7123 stanga laba7123 whatever goes here and even here
00:03:24:03:04 jonas laba324 and here


Last edited by tadasvu; 12-17-2008 at 04:19 PM..
# 6  
Old 12-17-2008
messy != incorrect

Or is it?
# 7  
Old 12-17-2008
Quote:
Originally Posted by vgersh99
messy != incorrect

Or is it?
Its corect, thanks very much for helping me, it works with the given example but if there the wirst word repeats in second file it gets like i have written above.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

3. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

6. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

7. Shell Programming and Scripting

merging of files.

Hi, I want to merge the two files on the basis of columns like... file 1 Data Key A 12 B 13 file2 Data Value A A1 A A2 B B1 B B2 (5 Replies)
Discussion started by: clx
5 Replies

8. Shell Programming and Scripting

merging two files

Hi everyone, I have two files which will be exactly same at first. After sometime there will be inserts in one file. My problem is how to reflect these changes in second file also. I found out that any compare and merge utility would do the job like, GNU " sdiff " command. But the... (14 Replies)
Discussion started by: rameshonline
14 Replies

9. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

10. UNIX for Dummies Questions & Answers

Merging files

Hi i have two files say file 1 contents are A B C D E I have file2 contents are B E F G C K I want to have new file like A B (4 Replies)
Discussion started by: ssuresh1999
4 Replies
Login or Register to Ask a Question