The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare Files and Output Difference Joesgrrrl UNIX for Dummies Questions & Answers 2 12-13-2008 09:51 PM
Compare two files and output diff to third file altamaha Shell Programming and Scripting 8 09-25-2008 10:42 AM
Compare 2 files and output result in pop up window hongsh UNIX for Dummies Questions & Answers 2 05-23-2008 09:50 AM
Compare 2 files and give uniq output rauphelhunter Shell Programming and Scripting 1 05-12-2008 04:47 PM
compare two col from 2 files, and output uniq from file 1 pp56825 Shell Programming and Scripting 2 01-10-2008 11:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-17-2009
cameleon cameleon is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 9
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
  #2 (permalink)  
Old 04-17-2009
tpietschmann tpietschmann is offline
Registered User
  
 

Join Date: Mar 2009
Location: Kansas City, MO
Posts: 18
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 (permalink)  
Old 04-17-2009
cameleon cameleon is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 9
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 12:53 PM..
  #4 (permalink)  
Old 04-20-2009
cameleon cameleon is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 9
Ping
  #5 (permalink)  
Old 04-22-2009
cameleon cameleon is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 9
Thanks

Last edited by cameleon; 04-22-2009 at 12:56 PM..
  #6 (permalink)  
Old 04-20-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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
  #7 (permalink)  
Old 04-20-2009
cameleon cameleon is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 9
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0