Compare 2 files and send output to other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files and send output to other
# 1  
Old 04-17-2009
Compare 2 files and send output to other

Hi,

In File1.txt I have text like:

23AA3424DD23|3423ff25sd5345| and so on

In File2.txt I have similar text as File1, but with ",":

23aa3424dd23,192.168.1.100, and so on

I wan to remove the pipes from File1 and select 5 fields, then remove "," from File2.txt and select 2 fields (IP's and MAC's), compare (line by line) the MAC's from File2 with the ones in File1, if equal then print those 5 fields from File1 with the correspondent IP from File2 in Final.txt

I can't figure it out using awk with variables, I'm using files:

Code:
awk -F"|" '{print $1, $2, $4, $5, $10}' File1.txt > temp1.txt

awk -F"," '{print $1, $2}' File2.txt|tr '[:lower:]' '[:upper:]'  > temp2.txt

egrep -f File2.txt File1.txt > Final.txt

and I was trying to compare $2 from File2 with the awk from File1, if equal print the the whole awk from File1 adding the correspondig $1 (IP) from File2 in Final.txt

I think I'm being clear Smilie
# 2  
Old 04-17-2009
Without knowing a full example of text here is an option.

You could wrap it in a loop:

Code:
awk -F"|" '{print $1, $2, $4, $5, $10}' File1.txt|tr '[:lower:]' '[:upper:]' > temp1.txt
awk -F"," '{print $1, $2}' File2.txt|tr '[:lower:]' '[:upper:]'  > temp2.txt
for MAC in `cat temp1.txt`
 do
  MAC2=`cut -d" " -f1 temp2.txt`
  IP=`cut -d" " -f2 temp2.txt`
  if [ "$MAC" = "$MAC2" ] ; then
   echo "`cat temp1.txt` - $IP" >> Final.txt
  fi
 done

You also seem to have some lower in File1.txt so I compensated and you mentioned $2 to compare when $1 is your MAC in File2.
# 3  
Old 04-17-2009
In File1.txt I have text like:

23AA3424DD23|3423ff25sd5345| and so on

In File2.txt I have similar text as File1, but with ",":

23aa3424dd23,192.168.1.100, and so on

If the second field from File2.txt matches the tenth field from File1.txt then write that line and append the correspondent IP from File2.txt in Final.txt

Edited: For better understanding.

Last edited by cameleon; 04-22-2009 at 01:53 PM..
# 4  
Old 04-20-2009
Ping Smilie
# 5  
Old 04-20-2009
Code:
nawk '
BEGIN {
  FS1="|"
  FS2=","
  FS=FS2
}
FNR==NR { a[$2]=$1;next}
FNR==1 {FS=FS1;$0=$0}
$10 in a {$0=$0 FS a[$10]; print}' file2 file1

# 6  
Old 04-20-2009
Thanks vgersh, I tested and it worked, but I had to edit the file for it to work, because the File2 is in lowercase.
I tried to insert a
Code:
tr '[:lower:]' '[:upper:]'

, can you give me a hand?
# 7  
Old 04-20-2009
Code:
nawk '
BEGIN {
  # FieldSeparator for file1
  FS1="|"

  # FieldSeparator for file2
  FS2=","

  # start with file2 => assign the appropriate FS
  FS=FS2
}
# if processing the FIRST file (specified on cli - file2), array "a" indexed by "toupper($2)" (second field) with the value of "$1" (first field)
# "next" => go to the next record/line
FNR==NR { a[toupper($2)]=$1;next}

# if we got here - you're processing the SECOND file from cli (file1)
# if on the FIRST line (FNR==1) set the FieldSeparator (FS) to the value of FS1 "|" and reevaluate the current record ($0=$0) given a new FS
FNR==1 {FS=FS1;$0=$0}

# if the TENTH field ($10) is in array "a" (index in "a"), then print the entire record/line suffixing it with the FieldSeparator (FS) and the
# value from array "a" indexed by the TENTH field ($10).
$10 in a {print $0 FS a[$10]}' file2 file1


Last edited by vgersh99; 04-20-2009 at 01:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Compare 2 files the send output to a file

Hallo Friends, I would like to compare two files, then write the difference between the two into output file then find a pattern then search for that pattern. -bash-3.2$ cat BS_Orig_20141112.csv|head -20 BW0159574451211141638275086@196.35.130.5 BW02043750712111491637691@196.35.130.5... (2 Replies)
Discussion started by: kekanap
2 Replies

3. UNIX for Dummies Questions & Answers

To compare two files,Output into a new file

Hi Please help me to compare two files and output into a new file file1.txt 15114933 |4001 15291649 |933502 15764675 |4316 15764678 |4316 15761974 |282501 15673104 |933505 15673577 |933505 15673098 |933505 15673096 |933505 15673092 |933505 15760705 ... (13 Replies)
Discussion started by: Ankita Talukdar
13 Replies

4. UNIX for Dummies Questions & Answers

Compare two files and output to new file

Hi, Please help How to compare two files- Any mismatches 2nd and 3rd column's values corresponding to 1st column. file1 15294024|Not Allowed|null 15291398|Not Allowed|null 15303292|Dropship (standard)|N 15303291|Dropship (standard)|N 15275561|Store Only|Y 15275560|Store Only|Y... (2 Replies)
Discussion started by: Ankita Talukdar
2 Replies

5. Shell Programming and Scripting

Compare two files and output in another file

I have two files ' 1st one ALIC-000352-B ALIC-000916-O DDS-STNGD FDH-PPO1-001 PFG-30601-001 2nd one 'ALIC-000352-B' 'ALIC-000916-O' 'DDS-STNGD' 'FDH-PPO1-001' (4 Replies)
Discussion started by: Pratik4891
4 Replies

6. Shell Programming and Scripting

want to send 4 files output in one mail

Hi All , i have a wrapper HTML script -FS_CHECK_HTML_SCIPT_WT_Statusbar.sh which read a TXT file (script_KB.txt) and sends output to a mail in tabular format. TXT file is having 6 columns and so wrapper script create a table with 6 columns and supplies those 6 columns data from TXT file .... (2 Replies)
Discussion started by: deepakiniimt
2 Replies

7. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

Compare two files and get output

Hi, I have two files, file1 and file2 and I need to compare them by line (exact match, order of the lines is not important) and get output with lines from file2 that are not found in file1 (not other way around). How do I do that? With grep or otherwise.. Thankyou (2 Replies)
Discussion started by: orp56
2 Replies

9. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

10. Shell Programming and Scripting

compare 2 files > output new to third

Hi, I have a question of comparing to files and output the result third file where file1 is the mainfile containing processed dir data and 2nd file grepīs dirīs data again (could be newer dirs comparing file1<file2) now i wanna make shure that output in file3 only contains newer dirs hx... (1 Reply)
Discussion started by: needle
1 Replies
Login or Register to Ask a Question