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
Help needed with Sort and uniq data asirohi Shell Programming and Scripting 4 08-17-2009 09:25 PM
How to replicate data using Uniq or awk ahjiefreak Shell Programming and Scripting 5 08-19-2008 02:25 AM
Compare 2 files and give uniq output rauphelhunter Shell Programming and Scripting 1 05-12-2008 05: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
Modify files amgo UNIX for Advanced & Expert Users 7 12-12-2005 09:19 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-24-2009
asirohi asirohi is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 56
Modify log files to get uniq data

Hello,

I have a log file that has following output as below.


Code:
LAP.sun5 CC
LAP.sun5 CQ
perl.sun5 CC
perl.sun5 CQ
TSLogger.sun5 CC
TSLogger.sun5 CQ
TSLogger.sun5 KR
WAS.sun5 CC
WAS.sun5 MT
WAS.sun5 CQ

I want to output to be in the way below, i tried using awk but could not do it.

Code:
LAP.sun5 CC CQ
perl.sun5 CC CQ
TSLogger.sun5 CC CQ KR
WAS.sun5 CC MT CQ

Please help . how do i do that

Thanks
Adsi

Last edited by asirohi; 08-24-2009 at 05:12 PM..
  #2 (permalink)  
Old 08-24-2009
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922

Code:
#!/bin/sh
for entry in `awk '{ print $1 }' < your.log.file | sort -u`
do
  string=""
  for result in `egrep "^$entry " your.log.file | awk '{ print $2 }'`
  do
    string="$result $string"
  done
  echo "$entry $string"
done

Exact grep syntax will depend on your version of grep available, egrep for Solaris, grep -e for most others, grep for the rest
  #3 (permalink)  
Old 08-24-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 552
Quote:
Originally Posted by asirohi View Post
...
I want to output to be in the way below, i tried using awk but could not do it.

Code:
LAP.sun5 CC CQ
perl.sun5 CC CQ
TSLogger.sun5 CC CQ KR
WAS.sun5 CC MT CQ

...

Code:
$ 
$ cat data.txt
LAP.sun5 CC
LAP.sun5 CQ
perl.sun5 CC
perl.sun5 CQ
TSLogger.sun5 CC
TSLogger.sun5 CQ
TSLogger.sun5 KR
WAS.sun5 CC
WAS.sun5 MT
WAS.sun5 CQ
$ 
$ awk '{if (prev == ""){printf("%s",$0)}
        else if($1 == prev){printf(" %s",$2)}
             else {printf("\n%s",$0)} prev=$1
       } END {printf "\n"}' data.txt
LAP.sun5 CC CQ
perl.sun5 CC CQ
TSLogger.sun5 CC CQ KR
WAS.sun5 CC MT CQ
$ 
$

tyler_durden
  #4 (permalink)  
Old 08-25-2009
asirohi asirohi is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 56
Hello Tyler,

Thanks for the wonderful explaination, that was superb. I have a question if(prev == "") and i dont want to display any thing what do i do. I asked this question because in the log files there are $1 fields that occur only once and i dont want to display them. For example
HTML Code:
[COLOR=Red]Linix CQ[/COLOR]
LAP.sun5 CC
LAP.sun5 CQ
perl.sun5 CC
perl.sun5 CQ
TSLogger.sun5 CC
TSLogger.sun5 CQ
TSLogger.sun5 KR
WAS.sun5 CC
WAS.sun5 MT
WAS.sun5 CQ
[COLOR=Red]Sun.log CC[/COLOR]

So the output should be as below and should leave $1 field 
if it has no previous value.

LAP.sun5 CC CQ
perl.sun5 CC CQ
TSLogger.sun5 CC CQ KR
WAS.sun5 CC MT CQ



Code:
$ awk '{if (prev == ""){printf("%s",$0)}
        else if($1 == prev){printf(" %s",$2)}
             else {printf("\n%s",$0)} prev=$1
       } END {printf "\n"}' data.txt


Last edited by asirohi; 08-25-2009 at 10:48 AM.. Reason: code tags, PLEASE!
  #5 (permalink)  
Old 08-25-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
A different approach

Code:
awk 'NF{_[$1]=$0}END{for(i in _)print _[i]}' file | sort

  #6 (permalink)  
Old 08-25-2009
asirohi asirohi is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 56
Hello Danmer,

That does not work, did it work for you?
  #7 (permalink)  
Old 08-25-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
Works for me, try to use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk).
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 04:47 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