Counting similar lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting similar lines
# 1  
Old 02-01-2010
Bug Counting similar lines

Hi,

I have a little problem with counting lines. I know similar topics from this forum, but they don't resolve my problem. I have file with lines like this:

Code:
2009-05-25 16:55:32,143 some text some regular expressions ect.
2009-05-25 16:55:32,144 some text.
2009-05-28 18:15:12,148 some error logs.
2009-05-28 18:15:12,148 some error logs.
2009-05-28 18:15:12,149 some error logs.
2009-05-28 18:15:12,150 some error logs.
2009-06-25 16:55:32,222 some text some regular expressions ect.
2009-06-25 16:55:32,223 some text some regular expressions ect.

So time stamp can be different. I want to count just messages which are the same in the line. I found this on forum:
Code:
#!/bin/ksh

cnt=0
while read line
do
   # set the variable contents
   ((cnt+=1))
   eval a${cnt}="\$line"

if [[ $cnt -eq 1 ]]
then
continue
fi

i=0
((snum=cnt-1))
v2=$(eval echo \"\$a$cnt\")
while (($i < $snum))
do
((i+=1))
v1=$(eval echo \"\$a${i}\")

# skip the line if it's equal to another
if [[ "$v1" = "$v2" ]]
then
((cnt-=1))
fi
done
done < my_file.log

# display the variables a1, a2,...a10
x=1
while (($x <= $cnt))
do
eval echo \"\$a${x}\"
((x+=1))
done

but how to cut of 2 first colums if I know that each line can have different number of words?

Thanks in advance!
# 2  
Old 02-01-2010
Code:
awk ' { tmp=""
           for(i=1; i<=NF; i++) {
               tmp=sprintf("%s %s", tmp, $i)
           }
           arr[tmp]++
        }
        END {
            for(i in arr) {
              print i, arr[i]
            }  
        } ' inputfile > outputfile

Try something like this
# 3  
Old 02-02-2010
counting similar lines

I modify script to cut a time stamp infront of the message:

Code:
#!/bin/ksh
cnt=0
while read line
do
   # set the variable contents
   ((cnt+=1))
   SHORTLN=`echo "$line" | sed 's/.*,[0-9]*//g'`
   eval a${cnt}="\$SHORTLN"
   # Added for test
   echo "After eval 1 a${cnt}"
   if [[ $cnt -eq 1 ]]
   then
      continue
   fi
   
   i=0
   ((snum=cnt-1))
   v2=$(eval echo \"\$a$cnt\" )
   #Added for test
   echo "v2 set to $v2"
   while (($i < $snum))
   do
      ((i+=1))
      v1=$(eval echo \"\$a${i}\")
      #Added for test
      echo "v1 set to $v1"
      
   # skip the line if it's equal to another
   if [[ "$v1" = "$v2" ]]
   then
     ((cnt-=1))
   fi
   done
done < my_file.log
# display the variables a1, a2,...a10
x=1
while (($x <= $cnt))
do
   eval echo \"\$a${x}\"
   ((x+=1))
done


Last edited by Franklin52; 02-02-2010 at 02:03 PM.. Reason: Please use code tags!
# 4  
Old 02-02-2010
Try this:

Code:
$ awk '{$1=$2=""}1' urfile |sort |uniq -c |sort -n
      1   some text.
      3   some text some regular expressions ect.
      4   some error logs.

# 5  
Old 02-02-2010
Code:
cut -f3- -d ' ' abc.txt | sort | uniq -c

HTH,
PL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Getting similar lines in two files

Hi, I need to compare the /etc/passwd files from 2 servers, and extract the users that are similar in these two files. I sorted the 2 files based on the user IDs (UID) (3rd column). I first sorted the files using the username (1st column), however when I use comm to compare the files there is no... (1 Reply)
Discussion started by: anaigini45
1 Replies

2. Shell Programming and Scripting

Count lines with similar tokens

I have 2 files, and I wish to count number of lines with this characteristic: if any token at line x in file1, is similar to a token at line x in file2. Here's an example: file1: ab, abc ef fg file2: ab cd ef gh In this case I wish to get 3. Note that token of file1 are... (3 Replies)
Discussion started by: Viernes
3 Replies

3. Shell Programming and Scripting

extracting lines from a file with similar first name

consider i have two files cat onlyviews1.sql CREATE VIEW V11 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V22 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V33 AS (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

Maximum Value of similar lines

Hi, Pretty new to scripting sed awk etc. I'm trying to speed up calculations of disk space allocation. I've extracted the data i want and cleaned it up but i cant figure out the final step. I need to discover a Maximum value of 1 field where the value of another field is the same using awk so... (4 Replies)
Discussion started by: imarcs
4 Replies

5. Shell Programming and Scripting

remove one of each similar lines in a file

Hello folks I have a question for you gurus of sed or grep (maybe awk, but I would prefer the first two) I have a file (f1) that says: (actually, these are not numbers but md5sum, but for simplicity, let's assume these numbers.) 1 2 3 4 5And I have a file (f2) that says 1|a 1|b 1|c 2|d... (3 Replies)
Discussion started by: tukuyomi
3 Replies

6. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies

7. Shell Programming and Scripting

merging similar lines

Greetings, I have been trying to merge the following lines: Sat. May 9 8:00 PM Sat. May 9 8:00 PM CW Sat. May 9 8:00 PM CW Cursed Sat. May 9 9:00 PM Sat. May 9 9:00 PM CW Sat. May 9 9:00 PM CW Sanctuary Sat. May 16 8:00 PM Sat. May 16 8:00 PM CW Sat. May 16 8:00 PM CW Sanctuary Sat. May... (2 Replies)
Discussion started by: adambot
2 Replies

8. Shell Programming and Scripting

Deleting the similar lines

Dear Friends myself Avinash working in bash shell The problem goes like this I have a file called work.txt assume that first colum=mac address second colum= IP third colum = port number ---------------------------------------- 00:12:23:34 192.168.50.1 2 00:12:23:35 192.168.50.1 5... (2 Replies)
Discussion started by: avi.skynet
2 Replies

9. Shell Programming and Scripting

Urgent : Merge similar lines

Hi, I have a file like this. please notice that ./usr/orders1/order_new_2627 appears more than once, thus needs to be merged. I would like to merge the lines where the first column match so the output should be like this: Please help (2 Replies)
Discussion started by: rakeshou
2 Replies

10. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies
Login or Register to Ask a Question