The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare 2 files.. amon Shell Programming and Scripting 8 06-23-2008 07:34 AM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 12:20 PM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 01:35 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 10:34 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 08:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 01-27-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,516
Stumble this Post!
I said GNU awk,
are you using GNU awk?
It's hard to troubleshoot, unless I can see the entire file1 and file2 content.
Could you also post the output from this commands:

Code:
patt="$(printf "id=\"%s\"|" $(<file2))" ; echo "${patt%|}"

Code:
$ awk --version| head -2
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.
$ cat file1
<object
type="user"
id="0000E2801BFD"
encryptedPassword=""
>
<checkListAttributes>
</checkListAttributes>
</object>
<object
type="user"
id="999999999999"
encryptedPassword=""
>
<checkListAttributes>
</checkListAttributes>
</object>

$ cat file2
000039BF228B
0000E2801BFD

$ awk '$0 ~ patt{print $0RS}' RS="</object>" patt="${patt%|}" file1
<object
type="user"
id="0000E2801BFD"
encryptedPassword=""
>
<checkListAttributes>
</checkListAttributes>
</object>
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 01-27-2007
Registered User
 

Join Date: Dec 2006
Posts: 58
Stumble this Post!
sorry, I was wrong to enter the command incorrectly. It works, comsumes a lot of computational power though. thanks!
would you explain your code?
Reply With Quote
  #10 (permalink)  
Old 01-27-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,516
Stumble this Post!
Quote:
Originally Posted by fredao
[...]
It works, comsumes a lot of computational power though. thanks!
would you explain your code?
Yep,
it's an "ugly" and "buggy" code (think what happens if your file2 is big ).
I'm not able to write a good code in 2 minutes .
The first command generate your pattern list with a various "or" ("|").
The second tests all the records (RS="</object>" assumed) in file1 against it.
Reply With Quote
  #11 (permalink)  
Old 01-27-2007
Registered User
 

Join Date: Dec 2006
Posts: 58
Stumble this Post!
comment

Quote:
Originally Posted by fredao
sorry, I was wrong to enter the command incorrectly. It works, comsumes a lot of computational power though. thanks!
would you explain your code?
nawk 'NR==FNR { keys[$1]++;next }; RS="</object>"; $3 in keys' file2 file1
Reply With Quote
  #12 (permalink)  
Old 01-27-2007
Registered User
 

Join Date: Dec 2006
Posts: 58
Stumble this Post!
Quote:
Originally Posted by radoulov
Yep,
it's an "ugly" and "buggy" code (think what happens if your file2 is big ).
I'm not able to write a good code in 2 minutes .
The first command generate your pattern list with a various "or" ("|").
The second tests all the records (RS="</object>" assumed) in file1 against it.
patt is a shell variable or gawk variable? as the 1st command seems to have nothing to do with awk? is there any link for this grammar?
Reply With Quote
  #13 (permalink)  
Old 01-28-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,516
Stumble this Post!
Quote:
Originally Posted by fredao
patt is a shell variable or gawk variable?
[...]
patt is a shell variable here:

Code:
patt="$(printf "id=\"%s\"|" $(<file2))"
... and becomes an awk variable here:

Code:
awk '$0 ~ patt{print $0RS}' RS="</object>" patt="${patt%|}" file1
Reply With Quote
  #14 (permalink)  
Old 01-28-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,516
Stumble this Post!
And, of course, (given your input format) with GNU grep:

Code:
grep -B2 -A5 -f file2 file1

Last edited by radoulov; 01-28-2007 at 07:07 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0