Comparison of two files in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison of two files in awk
# 8  
Old 07-26-2006
mmmmmmm... Still not clear for me.... Let me see If now I understand.
- You want to check one-to-one, or
- two-to-two?
Please, give me another example to make it clear Smilie
# 9  
Old 07-26-2006
Yah....
Its a one to one mapping between the files..

file1
-------
a;a;c;
d;f;g;

3;7;8;

file2
------
4;7;8;
3;4;7
a;a;c;
d;f;g;

success file1
-----------
a;a;c;
d;f;g;


fail file1
--------
3;7;8;

I want to get success and fail records of file1 in different file..

I dont need any information in file2.(You can take it as an mapping file like..)
# 10  
Old 07-26-2006
OK.
col3-to-col3 always or col3-to-colX or colX-to-colY?
In your example you are comparing whole lines with whole lines...
mmmmm:
Code:
#!/bin/bash

> success.txt
> fail.txt
comp1=($(cat text1.txt))
comp2=($(cat text2.txt))

for str in ${comp1[*]}; do
   i=0
   FOUND=no
   while (( $i < ${#comp2[*]} )); do
      if [[ $str = ${comp2[i]} ]]; then
         cat text1.txt | grep $str >> success.txt
         FOUND=yes
         break
      fi
      (( i += 1 ))
   done
   if [[ $FOUND = no ]]; then
      cat text1.txt | grep $str >> fail.txt
   fi
done

Which compares whole lines...

Last edited by grial; 07-26-2006 at 08:34 AM..
# 11  
Old 07-26-2006
Hi,

As i have said its a one-one mapping,but the columns can be dynamic.
It can be col X(file1)-col 1(file2) ,colX,colY(file1)-col1col2(file2).Is it possible with the current script.
# 12  
Old 07-26-2006
while read line;do
first=`echo $line | cut -d ";" -f1`
third=`echo $line | cut -d ";" -f3`
while read var; do
result=`echo $var | awk -F";" -v first=${first} -v third=${third} '{if($1~first && $3~third) print 1; else print 0}'`
if [[ result -eq 1 ]]; then
break
fi
done < file2

if [[ $result -eq 1 ]]; then
echo $line >> found
else
echo $line >> notfound
fi
done < file1
# 13  
Old 07-26-2006
Hi, Sukumar
This is simple solution for your problem.

#!/bin/bash

while read var1
do
colf1=` echo $var1 | cut -d";" -f 3,4`
grep $colf1 file2 >> sucessfile1
done < file1
grep -vf sucessfile1 file1 >> failfile1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk comparison using multiple files

Hi, I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below. Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in... (2 Replies)
Discussion started by: grv
2 Replies

2. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

3. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

4. Shell Programming and Scripting

Need help with simple comparison in AWK

Hi, I'm new to AWK and I'm having problems comparing a field to a string variable. /ARTIST/ {x = $2} $1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Discussion started by: klusps
7 Replies

5. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

6. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

7. Shell Programming and Scripting

awk comparison

Hello all, Probably a very simple question, I am stuck with a small part of a code: I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing: awk -F'\t' '{if ($6 > a)a=$6}END{for (i in a) print i"\t"a}' ... (4 Replies)
Discussion started by: jaysean
4 Replies

8. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

9. Shell Programming and Scripting

Awk Comparison of 2 specific files

Hi Everybody, I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help. I would like to compare 2 files in an awk script. Both files have different paths. The awk script call should look like that awk -f... (7 Replies)
Discussion started by: hhoosscchhii
7 Replies

10. Shell Programming and Scripting

String Comparison between two files using awk

I have two files with field seperator as "~". File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank... (7 Replies)
Discussion started by: rudoraj
7 Replies
Login or Register to Ask a Question