Sort uniq or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort uniq or awk
# 1  
Old 01-16-2013
Sort uniq or awk

Hi again,

I have files with the following contents

Code:
datetime,ip1,port1,ip2,port2,number

How would I find out how many times ip1 field shows up a particular file? Then how would I find out how many time ip1 and port 2 shows up?

Please mind the file may contain 100k lines.

Last edited by Scott; 01-16-2013 at 01:10 PM.. Reason: Code tags
# 2  
Old 01-16-2013
I don't understand what you're trying to do.

Are you saying that you have a comma separated file, but some lines have less than 5 commas and you want to know how many lines have at least one comma but less than 4 commas?
# 3  
Old 01-16-2013
Post a sample of the input and the output as we cant read your mind...
# 4  
Old 01-16-2013
Quote:
Originally Posted by LDHB2012
Hi again,

I have files with the following contents

Code:
datetime,ip1,port1,ip2,port2,number

How would I find out how many times ip1 field shows up a particular file? Then how would I find out how many time ip1 and port 2 shows up?

Please mind the file may contain 100k lines.
Code:
grep ip1 filename | wc -l

grep ip1 filename | grep port2 | wc -l


Last edited by Scott; 01-16-2013 at 01:18 PM.. Reason: Code tags...
# 5  
Old 01-16-2013
Quote:
Originally Posted by jnojr
Quote:
Originally Posted by LDHB2012
Hi again,

I have files with the following contents


Code:
datetime,ip1,port1,ip2,port2,number

How would I find out how many times ip1 field shows up a particular file? Then how would I find out how many time ip1 and port 2 shows up?

Please mind the file may contain 100k lines.
Code:
grep ip1 filename | wc -l
grep ip1 filename | grep port2 | wc -l

Note that neither your script above nor the script below will find out how many times "port 2" shows up; both script will find out how many times "port2" shows up.

Since you allow both ip1 and port2 to appear anywhere in a line whether or not a field has other characters before or after "ip1" and "port2" in the field and no matter which field contains them, I don't see any way to use sort or uniq to do what you want. This following awk script should be much more efficient than running wc twice and grep three times:
Code:
awk '!/ip1/{next}
        {c++}
/port2/ {c2++}
END     {printf("%d\n%d\n", c, c2)}' filename

# 6  
Old 01-17-2013
Making wild guesses that the requestor wants any possible combination of field 2 and field 5 in a large comma separated file of numbers, I get at
Code:
$ awk  'BEGIN {SUBSEP=":"}
         NR==1 {next}
         {IP1[$2]++;PORT2[$2,$5]++}
         END {for (i in PORT2) {x=index(i,SUBSEP); j=substr(i,1,x-1); print "IP1=" j ": " IP1[j] ", PORT2=" substr(i,x+1)":  "PORT2[i]}}
        ' FS="," file
IP1=1: 1, PORT2=5:  1
IP1=2: 2, PORT2=5:  1
IP1=2: 2, PORT2=6:  1
IP1=3: 3, PORT2=5:  2
IP1=3: 3, PORT2=6:  1

Input file would be sth like
Code:
datetime,ip1,port1,ip2,port2,number
9,1,8,7,5,4 
9,2,8,7,5,4
9,3,8,7,5,4
9,3,8,7,5,4
9,2,8,7,6,4
9,3,8,7,6,4

# 7  
Old 01-17-2013
Thank you all for replying and I apologize for not making myself clear when asking this question. I think RudiC may have got it the closest, but I'm still not sure it's exactly what I'm looking for.

So, in my very large file, I have 6 fields and 300,000 lines of the same type of fields, obviously each line will contain different information in each field. But some lines will contain the same value in certain fields.

Here's my original qusetion:
"How would I find out how many times ip1 field shows up a particular file? Then how would I find out how many time ip1 and port 2 shows up?"

To explain this better: ip1 is not a header, but simply represents an IP address in field 2. Port2 simply represents a port number in field 5, associated with IP address in field 4. My goal is find out how many similar instances in field 2(ip1), shows up in this one file. THEN, I want to know how many times that same IP address shows up with the SAME port number (lets just say field 5).

I know awk could probably do this. But I was wondering if sort or uniq can recognize field values, as awk would recognize the field ip1, as $2.

I hope that clears it up. Thanks again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort & Uniq -u

Hi All, Below the actual file which i like to sort and Uniq -u /opt/oracle/work/Antony/Shell_Script> cat emp.1st 2233|a.k. shukula |g.m. |sales |12/12/52 |6000 1006|chanchal singhvi |director |sales |03/09/38 |6700... (8 Replies)
Discussion started by: Antony Ankrose
8 Replies

2. UNIX for Dummies Questions & Answers

Uniq and sort -u

Hello all, Need to pick your brains, I have a 10Gb file where each row is a name, I am expecting about 50 names in total. So there are a lot of repetitions in clusters. So I want to do a sort -u file Will it be considerably faster or slower to use a uniq before piping it to sort... (3 Replies)
Discussion started by: senhia83
3 Replies

3. Shell Programming and Scripting

Uniq or sort -u or similar only between { }

Hi ! I am trying to remove doubbled entrys in a textfile only between delimiters. Like that example but i dont know how to do that with sort or similar. input: { aaa aaa } { aaa aaa } output: { aaa } { (8 Replies)
Discussion started by: fugitivus
8 Replies

4. Shell Programming and Scripting

Sort field and uniq

I have a flatfile A.txt 2012/12/04 14:06:07 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:07:22 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:13:27 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 14:07:39 |rain|Boards 1|tampa|merced|merced11 How do i sort and get... (3 Replies)
Discussion started by: sabercats
3 Replies

5. Shell Programming and Scripting

Sort and uniq after comparision

Hi All, I have a text file with the format shown below. Some of the records are duplicated with the only exception being date (Field 15). I want to compare all duplicate records using subscriber number (field 7) and keep only those records with greater date. ... (1 Reply)
Discussion started by: nua7
1 Replies

6. Shell Programming and Scripting

Text Proccessing with sort,uniq,awk

Hello, I have a log file with the following input: X , ID , Date, Time, Y 01,01368,2010-12-02,09:07:00,Pass 01,01368,2010-12-02,10:54:00,Pass 01,01368,2010-12-02,13:07:04,Pass 01,01368,2010-12-02,18:54:01,Pass 01,01368,2010-12-03,09:02:00,Pass 01,01368,2010-12-03,13:53:00,Pass... (12 Replies)
Discussion started by: rollyah
12 Replies

7. Shell Programming and Scripting

sort | uniq question

Hello, I have a large data file: 1234 8888 bbb 2745 8888 bbb 9489 8888 bbb 1234 8888 aaa 4838 8888 aaa 3977 8888 aaa I need to remove duplicate lines (where the first column is the duplicate). I have been using: sort file.txt | uniq -w4 > newfile.txt However, it seems to keep the... (11 Replies)
Discussion started by: palex
11 Replies

8. Shell Programming and Scripting

Help with Uniq and sort

The key is first field i want only uniq record for the first field in file. I want the output as or output as Appreciate help on this (4 Replies)
Discussion started by: pinnacle
4 Replies

9. Shell Programming and Scripting

Sort, Uniq, Duplicates

Input File is : ------------- 25060008,0040,03, 25136437,0030,03, 25069457,0040,02, 80303438,0014,03,1st 80321837,0009,03,1st 80321977,0009,03,1st 80341345,0007,03,1st 84176527,0047,03,1st 84176527,0047,03, 20000735,0018,03,1st 25060008,0040,03, I am using the following in the script... (5 Replies)
Discussion started by: Amruta Pitkar
5 Replies

10. UNIX for Dummies Questions & Answers

sort/uniq

I have a file: Fred Fred Fred Jim Fred Jim Jim If sort is executed on the listed file, shouldn't the output be?: Fred Fred Fred Fred Jim Jim Jim (3 Replies)
Discussion started by: jimmyflip
3 Replies
Login or Register to Ask a Question