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
Awking!! Printing decimal output is struck divzz Shell Programming and Scripting 4 05-28-2008 08:02 AM
Splitting huge XML Files into fixsized wellformed parts Malapha Shell Programming and Scripting 0 03-17-2008 11:35 AM
grepping many values from same files Sreejith_VK Shell Programming and Scripting 2 03-15-2008 08:41 AM
grepping lines out of files mariner UNIX for Advanced & Expert Users 2 09-24-2004 08:12 AM
diff 2 files; output diff's to 3rd file blt123 Shell Programming and Scripting 2 05-28-2002 11:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
awking and grepping parts of files: the 'super diff'

OKAY----

Here's what I must do.
I have two files. I need to compare the two files such as with the diff command. I am adding FILENEW to FILEOLD

If fields $1, $2, $5, and 6 are the same, then I don't want to add FILENEW records to FILEOLD.
If they are not, then append the lines.

Is this possible in the UNIX scripting world? It seems that this could be done in AIX. I also have Perl as a last resort. Looking hard, wondered if anyone has done something like this before.

  #2 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
it may be sickboys entry is similar...on phone will check when I get off
  #3 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
Does anybody want to take a crack at this?
My slow brain is simply not understanding.

If for example:

awk 'BEGIN{FS=OFS=","}
NR==FNR{a[$1]=$0; next}
a[$1]{$0=$0 RS a[$1]}{print}' file2 file1

Adds a line to file1 if the first field matches in both files.
How do you only print the line if say, fields1, 2, 5, and 6 match both files?
And of course, I would have to reverse that (i.e., only add the lines to file1 if fields 1, 2, 5, and 6 are NOT matched between file1 and file2)

Obviously the first FNR line is file2, a[$1] being the first field.
The second line then matches a[$1] and appends a record to the output of file1.

Thanks much for any assistance.
  #4 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
File1:
----------------------------
apples,pears,bananas,oranges,grapefruit,watermelon,pineapple
epples,pears,bananas,oranges,grapefruit,watermelon,pineapple
epples,paars,bananas,oranges,grapefruit,watermelon,pineapple
graby,epples,bananas,oranges,grapefruit,watermelon,pineapple


File2:
----------------------------
pearsrs,apples,bananers,orangas,grapefruit,graby,pineapples
apples,pears,bandanas,orangatangs,grapefruit,watermelon,wineapple



Wanted output: (File1: appended: only add records when fields 1, 2, 5 and 6 DO NOT match)
-----------------------------
apples,pears,bananas,oranges,grapefruit,watermelon,pineapple
epples,pears,bananas,oranges,grapefruit,watermelon,pineapple
epples,paars,bananas,oranges,grapefruit,watermelon,pineapple
graby,epples,bananas,oranges,grapefruit,watermelon,pineapple
pearsrs,apples,bananers,orangas,grapefruit,graby,pineapples
  #5 (permalink)  
Old 07-17-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
nawk -f jeff.awk FILENEW FILEOLD

jeff.awk:
Code:
{ idx = $1 SUBSEP $2 SUBSEP $5 SUBSEP $6 }
NR==FNR { fnew[ idx ] = $0; next }
{
   if ( !(idx in fnew) )
      $0 = $0 ORS fold[idx]
}
1

Last edited by vgersh99; 07-17-2008 at 03:30 PM..
  #6 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
Basically what this is is a logfile that has alerts from a program.
The entries get dropped into a file continually. But some entries have the same date, transaction ID, location, alert code and keep getting dropped in over and over (like until the problem is fixed)

The awk filter is to create a 2nd file that just adds new messages if they are not the same date, ID, location, code, and run on a continual basis filtering these out.
  #7 (permalink)  
Old 07-17-2008
jeffpas jeffpas is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 60
Thanks
But something is awry.

OKAY, If I do:
nawk -f jeffawk file2 file1
------------------------
apples,pears,bananas,oranges,grapefruit,watermelon,pineapple
epples,pears,bananas,oranges,grapefruit,watermelon,pineapple

epples,paars,bananas,oranges,grapefruit,watermelon,pineapple

graby,epples,bananas,oranges,grapefruit,watermelon,pineapple

This is basically a cat of file1.


If I do:
nawk -f jeffawk file1 file2
I get the following:
-----------------------
pearsrs,apples,bananers,orangas,grapefruit,graby,pineapples

apples,pears,bananas,oranges,grapefruit,watermelon,pineapple



The first line, 'pearsrs', is right on the money. But this is basically a cat of file2.


Not sure
? ? ? ?

Last edited by jeffpas; 07-17-2008 at 04:19 PM..
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, grep

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 01:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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