Please Help (Urgent) - Comparing 2 files with "awk"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please Help (Urgent) - Comparing 2 files with "awk"
# 1  
Old 03-28-2005
Please Help (Urgent) - Comparing 2 files with "awk"

Hi,

I would be grateful for any assistance in my situation. As a new comer to UNIX, I have tried, but in vain, to search for scripts or methods for this problem.

I need to compare 2 files. A field in the master is to be tested for a match in the slave. Once a match is found, a variable stated in a field in the master is to be added to a new field in the slave.

To make things more confusing, the data in the slave cannot be sorted which rules out the 'paste' command. An illustration is shown.

master file
A B C
D E F
G H I
J K L
M N O

slave file
X A V $4(F)
Z D Y $4(C)
U G T $4(I)

Field $2 in slave is to be tested for a match in master's field 1. Once matched, variables C F and I are to be copied to filed $4 into each respective line.

This problem has been bugging me for the past 3 days. I'd appreciate any advice.

Many Thanks in advance!!

Last edited by zom_chp; 03-28-2005 at 10:13 AM..
# 2  
Old 03-28-2005
how do you decide what letter (C, F or I) is to be added?
I think I'm missing something here - can you explain a bit clearer?
# 3  
Old 03-28-2005
I apologise for the mistake - typing error. It is to show that Slave cannot be sorted. It should have been

master file
A B C
D E F
G H I
J K L
M N O

slave file
X D V $4(F)
Z A Y $4(C)
U G T $4(I)


Slave line (X A V) field $2 is first tested for matching with field $1 of Master line (A B C). The testing, i believe, can be vice versa - master tested with slave

If both fields matched, the string/var in field $3 of Master is copied into a new field, $4, of Slave. If unmatched, it is left empty.

Thank you

Last edited by zom_chp; 03-28-2005 at 11:31 AM..
# 4  
Old 03-28-2005
Your explanation is confusing given the sample lines...., but I think that's what you want:

nawk -f zom.awk master slave

here's zom.awk:
Code:
NR == FNR { arr[$1]=$NF;next }
$2 in arr { $(NF+1) = arr[$2]}
1

# 5  
Old 03-28-2005
Thank you very much for the fast reply. I would like to ask another question related to the first.

What if,

Master
$1 = Write(0xc001)

Slave
$2 = 0xc001

These fields, for my case, match due to similar hex codes. What should I do?

Thanks again.
# 6  
Old 03-28-2005
Quote:
Originally Posted by zom_chp
Thank you very much for the fast reply. I would like to ask another question related to the first.

What if,

Master
$1 = Write(0xc001)

Slave
$2 = 0xc001

These fields, for my case, match due to similar hex codes. What should I do?

Thanks again.
pls post sample files - both master and slave - AND a desired output.
# 7  
Old 03-28-2005
Attached are the sample master and slave files.
The desired outputs are commented in the slave file.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

6. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

7. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y" #!/bin/bash du -hs $1 while read SIZE ENTRY do if ; then find $1 -mtime +$2 -exec rm -f {} \; echo "Files older than $2 days deleted" else echo "free Space available"... (4 Replies)
Discussion started by: JamesCarter
4 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question