Help merging two files if search pattern true.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help merging two files if search pattern true.
# 1  
Old 10-29-2009
Help merging two files if search pattern true.

Hello everyone,

I've been reading this forum whenever I had a problem with AWK but I can't seem to find how to solve my problem.

What I would like to do is the following:

I have a first file with two columns, on the first one is a certain name and in the second one, another corresponding name.

In my second file, I would like to search for a certain pattern, and if that pattern is found, search $3 of this line in the second row of file1 and replace it with the corresponding $1 of file 1.


I can always do fairly well with awk on one file, but I don't know how to use two files, I'm a bit lost with the NR==FNR thingy, so your help would be very much appreciated.

Here is what file1 look like:

Code:
HAL_MONTxData10    HAL_MONTxData10.HAL_IOInterface
HAL_MONTxData11    HAL_MONTxData11.HAL_IOInterface
...

And here is what file2 looks like:
Code:
...
  /begin MEASUREMENT HAL_MONTxData10.HAL_IOInterface 
   "" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000A8C8
  /end MEASUREMENT 
  /begin MEASUREMENT HAL_MONTxData11.HAL_IOInterface
   "" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000A8CA
  /end MEASUREMENT 
...

So, to sum things up I would like my return file to be like:

Code:
  /begin MEASUREMENT HAL_MONTxData10
   "" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000A8C8
  /end MEASUREMENT 
  /begin MEASUREMENT HAL_MONTxData11 
   "" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000A8CA
  /end MEASUREMENT


Thanks a lot for your help.

Teroc

Last edited by Franklin52; 10-29-2009 at 09:07 AM.. Reason: Please use code tags!
# 2  
Old 10-29-2009
something like this :

Code:
cat f1 :

HAL_MONTxData10 HAL_MONTxData10.HAL_IOInterface
HAL_MONTxData11 HAL_MONTxData11.HAL_IOInterface

other data in file2.

awk 'NR==FNR {a[$2]=$1} { if ($3 in a) {$3=a[$3]} }1' f1 f2

# 3  
Old 10-29-2009
Thank you it did the trick, I think I understand how that works now. Thanks for the lesson Smilie

One last thing, when I run this script, the file1 is written in the ouput before the corrected file2. Why? NR==FNR {a[$2]=$1} isn't supposed to output anything, is it?

thanks again.
# 4  
Old 10-29-2009
ohhh..

Sorry missed a bit.

Code:
awk 'NR==FNR {a[$2]=$1;next} { if ($3 in a) {$3=a[$3]} }1' f1 f2

# 5  
Old 10-29-2009
Thank you a lot Panyam!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Pattern search multiple files

#!/usr/bin/ksh a="Run successfully" cd $APPS ls -l *.txt | while read $txt do if then cp $APPS/$txt cp $hist/$txt else rm $APPS/$txt echo "Files has been removed" fi done New in shell script please help me out Around 100 txt files in $APPS dir i want to search pattern from... (8 Replies)
Discussion started by: Kalia
8 Replies

3. Shell Programming and Scripting

Search pattern in today's files only

Hi Friends, I am in search of unix command which can search a particular pattern in all files which are created/modified today ONLY. Which is the best way to achieve this? Thanks in advance. (1 Reply)
Discussion started by: Nakul_sh
1 Replies

4. Shell Programming and Scripting

Checking for pattern and merging files

I am trying to perform the following action. 1. A script runs the 'last' command for some users and prints the output to a file. $ cat last_users.log oracle pts/17 10.120.xxx.xxx Jun 28 14:42 - 18:01 (03:19) oracle pts/11 10.120.xxx.xxx Jun 28 14:28 - 20:17... (2 Replies)
Discussion started by: Nagesh_1985
2 Replies

5. Shell Programming and Scripting

Merging two files without any common pattern

Hi I have file1 as IJU_NSOMOW; SOWWOD_TWUIQ; and file2 as how are you?; fine there; Now my problem is i need the output file as IJU_NSOMOW; how are you?; SOWWOD_TWUIQ; fine there; (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

7. Shell Programming and Scripting

merging of 2 files in a particular pattern

how can i merge to files containing some random sort of numbers to a separate file .. file1 11111 10111 11011 file2 00000 01010 10101 file 3 11111_00000 10111_01010 11011_10101 Please let me know how to do this? (1 Reply)
Discussion started by: dll_fpga
1 Replies

8. Shell Programming and Scripting

search files which doesnot match pattern ?

Hi I need a command to search files in a directory which does not match with pattern .. Plz send me this Ex : Test is directory and has some 10 files with different name all are with *.dat extension , need to search files which doesnot contain word "Dummy file". Thanks (6 Replies)
Discussion started by: madankumar
6 Replies

9. UNIX for Advanced & Expert Users

pattern search between 2 files

Afternoon guys, I have 2 files, 1.txt and 2.txt containing employee numbers. the 1st file (1.txt) is an extract from sybase with active employee numbers, the 2nd (2.txt) is a scan from the sybase log containing successfull logins *** which i have already mined and now contains only employee... (5 Replies)
Discussion started by: Jefferson333
5 Replies

10. UNIX for Dummies Questions & Answers

search all files for a pattern

Hi there, I am looking for a shell script which recursively searches all the files under all the directories for a pattern specified in the script. For e.g., i am looking forward to search for the file names which contains numbers of the form 001*****. Thanks in advance, Naik (4 Replies)
Discussion started by: ynaik002
4 Replies
Login or Register to Ask a Question