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
Remove Similar entries in a File Nysif Steve UNIX for Advanced & Expert Users 2 03-13-2009 03:50 AM
vi to remove lines in file kjbaumann UNIX for Dummies Questions & Answers 2 03-13-2009 03:40 AM
remove lines from file bluemoon1 Shell Programming and Scripting 9 11-09-2007 01:40 AM
Remove Duplicate lines from File Nysif Steve UNIX for Dummies Questions & Answers 18 09-09-2007 08:57 AM
Remove Duplicate Lines in File Teh Tiack Ein Shell Programming and Scripting 5 01-12-2006 08:30 AM

Closed Thread
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 08-31-2007
Nysif Steve Nysif Steve is offline
Registered User
  
 

Join Date: Aug 2007
Location: Albany, NY
Posts: 26
Remove Similar Lines from a File

I have a log file "logreport" that contains several lines as seen below:


04:20:00 /usr/lib/snmp/snmpdx: [ID 702911 daemon.error] Agent snmpd appeared dead but responded to ping
06:38:08 /usr/lib/snmp/snmpdx: [ID 702911 daemon.error] Agent snmpd appeared dead but responded to ping
07:11:05 /usr/lib/snmp/snmpdx: [ID 702911 daemon.error] Agent snmpd appeared dead but responded to ping


I would like to edit the report to remove entries that report duplicate events (event being the portion highlighted in red). I have no knowledge of what the events will be or how long they are. I am trying to produce an output close to what is seen below:


04:20:00 /usr/lib/snmp/snmpdx: [ID 702911 daemon.error] Agent snmpd appeared dead but responded to ping
This Error was reproduced 2 times
  #2 (permalink)  
Old 09-02-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,420
Try and adapt the following awk program.
steve.awk :
Code:
{
   if (match($0, /\[/) == 0) {
      Lines[++LinesCount] = $0;
      LineIds[LinesCount] = ""
      next;
   }

   id  = substr($0, RSTART);

   if (++Ids[id] == 1) {
      Lines[++LinesCount] = $0;
      LineIds[LinesCount] = id;
   }
}

END {
   for (i=1; i<=LinesCount; i++) {
      print Lines[i];
      if (id = LineIds[i]) {
         if (Ids[id] > 1) {
            print "This Error was reproduced", Ids[id], "times";
         }
      }
   }
}
Execute the awk program :
Code:
awk -f steve.awk logreport
Jean-Pierre.
  #3 (permalink)  
Old 09-03-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
You can use --

cat logfile | sort | uniq -c > newlogfile

Thanks
Namish
  #4 (permalink)  
Old 09-04-2007
Nysif Steve Nysif Steve is offline
Registered User
  
 

Join Date: Aug 2007
Location: Albany, NY
Posts: 26
I ended up using the code:
Code:
cat logfile | sort | uniq -c -n6 >> logreport
For the uniq command the -c flag will print the number of occurences before each line while the -n6 flag will ignore the first 6 fields for comparison. The end result is exactly what I needed. Thank you for your help everyone.
  #5 (permalink)  
Old 03-13-2009
uniesh uniesh is offline
Banned
  
 

Join Date: Mar 2009
Posts: 16
cat file1|sort|uniq -c|cut -f2 >file2
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 08:29 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