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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Based on num of records in file1 need to check records in file2 to set some condns mavesum Shell Programming and Scripting 3 11-26-2008 10:48 AM
Using a variable to select records with awk joeyg Shell Programming and Scripting 5 09-26-2008 11:48 AM
Script needed to select and delete lower case and mixed case records abhilash mn Shell Programming and Scripting 1 03-17-2008 08:00 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01:15 PM
Select records based on search criteria on first column shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-02-2005 01:49 PM

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

Join Date: Jul 2008
Posts: 9
select records from one file based on a second file

Hi all:

I have two files:

file1:

Code:
74 DS  9871 199009871    1 1990  4  1 165200 Sc
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
29 CS   563 198700563  280 1987  7 27 043900 Ic
pr de te sa ox
      9.0      8.9  12.2700  35.6500    6.060
     11.0     10.9  12.2100  35.6490    6.100
**
74 DS  9871 199009871    8 1990  4  3 161500 Sc
pr de te sa ox
      1.0      1.0  13.4500  35.7420    5.940
      3.0      3.0  13.4500  35.7420    5.950
**
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb
pr de te sa
      2.0      2.0  15.3300  35.5920
      3.0      3.0  15.3300  35.5920
**

and file2:

Code:
29 CS   563 198700563  280 1987  7 27 043900 Ic
74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb

What I need is to retain from file1 the full records pointed in file 2 (not need to be ordered) in such as:


Code:
74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
29 CS   563 198700563  280 1987  7 27 043900 Ic
pr de te sa ox
      9.0      8.9  12.2700  35.6500    6.060
     11.0     10.9  12.2100  35.6490    6.100
**
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb
pr de te sa
      2.0      2.0  15.3300  35.5920
      3.0      3.0  15.3300  35.5920
**

I have tried several approaches with awk oneliners, but didn't succeed because don't know how to deal with multiple files each with dissimilar ORS and FS. That is for file1 RS and ORS are the awk defaults whereas for file2:

Code:
{RS="\\*\\*\n+";ORS="**\n"}.

Do you have a clue on how to proceed?

Thanks,

r.-

Last edited by rleal; 07-17-2009 at 05:42 AM.. Reason: bad post title
  #2 (permalink)  
Old 07-17-2009
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,411
Try...
Code:
awk 'FNR==NR{a[$0]=$0;next}($0 in a),$0=="**"' file2 file1

  #3 (permalink)  
Old 07-17-2009
rleal rleal is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 9
That was absolutely brilliant! Thanks so much.

r.-
  #4 (permalink)  
Old 07-17-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
Can you please explain your code, I always get confused while working with two files in awk.
Thanks,
  #5 (permalink)  
Old 07-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Hi Rakesh,


Code:
FNR==NR{a[$0]=$0;next}

is used for storing the first file (here file2) content into an array "a" based on the key "$0" ( which is a complete row).


Code:
($0 in a),$0=="**"'

The above code is verifying whether each row from the file1 is present in the array or not ?.if present print it till the row equals to "**".

Ygor:

Please let me know if i am wrong somewhere.
  #6 (permalink)  
Old 07-17-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
Quote:
Originally Posted by panyam View Post
Hi Rakesh,


Code:
FNR==NR{a[$0]=$0;next}

is used for storing the first file (here file2) content into an array "a" based on the key "$0" ( which is a complete row).

Ygor:

Please let me know if i am wrong somewhere.
And this is because FNR is reset for each file read...right?
  #7 (permalink)  
Old 07-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Quote:
Originally Posted by rakeshawasthi View Post
And this is because FNR is reset for each file read...right?

Sorry i forgot to mention .Yes. "NR" will be keep on incrementing where as "FNR" will reset once a file processing is over and re start again for the next file.
Reply

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 05:54 AM.


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