Determine differient IP in a very long record.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine differient IP in a very long record.
# 1  
Old 06-22-2009
Determine differient IP in a very long record.

Hi Everyone,

I have a txt file with 40k lines.
111 1.1.1.1
111 1.1.1.1
111 2.2.2.2
111 1.1.1.1
111 1.1.1.1

How would use perl to easy have IP list, the final result will be:
1.1.1.1,2.2.2.2,

I can only think using if to determine one by one through all 40k, this is working, but thought not efficient. Please advice.

Thanks
# 2  
Old 06-22-2009
Code:
perl -lane'
  push @f, $F[1] unless $x{$F[1]}++;
  print join ",", @f if eof
  ' infile

Or if you really want the final ,:

Code:
perl -ane'
  print $F[1], "," unless $x{$F[1]}++;
  print $/ if eof
  ' infile

# 3  
Old 06-22-2009
Thanks radoulov, it works Smilie.

I have another file:
312009 1 0620233743 0620233748 20090620235854 20.7.22.14 aaa
312009 2 0620233743 0620233748 20090620235854 20.7.22.14 aaa
312009 3 0620233743 0620233748 20090620235854 20.7.22.14 aaa
312009 5 0620233743 0620233748 20090620235854 21.7.22.14 aaa
312009 2 0620233743 0620233748 20090620235854 20.7.22.14 aaa

I would like to hav a result of:
20.7.22.1 8
21.7.22.1 5

i did:
perl -F' ' -lane '$h{$F[5]} += $F[1]; END{print "$_: $h{$_}" for sort keys %h}' a.txt

But failed, please advice Smilie Thanks




Instead of
[root@localhost ~]# perl -ane'
print $F[1], "," unless $x{$F[1]}++;
print $/ if eof
' a.txt

if i have a file a.pl, how can i put this command into that file?

Thanks

Last edited by jimmy_y; 06-22-2009 at 03:51 PM..
# 4  
Old 06-22-2009
You mean 20.7.22.14, not 20.7.22.1, rigth?

Code:
perl -ane'
  $x{$F[5]} += $F[1];
  print map "$_ $x{$_}\n", sort { $x{$b} <=> $x{$a} } keys %x
    if eof
  ' infile


Last edited by radoulov; 06-22-2009 at 03:57 PM.. Reason: corrected
# 5  
Old 06-22-2009
Quote:
Originally Posted by radoulov
You mean 20.7.22.14, not 20.7.22.1, rigth?
Sorry sorry, yes it is 20.7.22.14
# 6  
Old 06-22-2009
Quote:
Originally Posted by jimmy_y
[..]
if i have a file a.pl, how can i put this command into that file?
Use this code as a the Perl script:

Code:
#!/usr/bin/env perl

use warnings;
use strict;

my %x;

while (<>) {
  my @F = split;
  $x{$F[5]} += $F[1];
}

print map "$_ $x{$_}\n", 
  sort { $x{$b} <=> $x{$a} } keys %x;

# 7  
Old 06-22-2009
Quote:
Originally Posted by radoulov
You mean 20.7.22.14, not 20.7.22.1, rigth?

Code:
perl -ane'
  $x{$F[5]} += $F[1];
  print map "$_ $x{$_}\n", sort { $x{$b} <=> $x{$a} } keys %x
    if eof
  ' infile

Thanks radoulov Smilie it works so perfect just few seconds to process my file, inside has 30k lines.

If i have a.pl, how would I transfer this code into that perl file? Smilie Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Long file record

riends I have the following problem: test.txt I have a file that has the following contents: is a fixed-length file to the end of the number 12 has spaces, so that it is fixed length 123456789 123456789 123456789 12 This code shows me the length of each record, but in... (2 Replies)
Discussion started by: tricampeon81
2 Replies

2. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

3. UNIX for Dummies Questions & Answers

awk: record too long

Hi All , I am getting record too long for the below command . nawk -F\" '{a=a" "$2} END{for(i in a) print i,a }' test|sort|awk '{for(i=1;i<=NF;i++) t=t"\t"$i;if(NF>max)max=NF} END{for(i=1;i<=max;i++)print t }' File test has 850 records ... Please help.. (2 Replies)
Discussion started by: saj
2 Replies

4. Shell Programming and Scripting

Record too long error while running awk

I have written below script to add substring in a file containing fixed length record, but when I run below script I get error as: ERROR: awk: record `22503004502488344040...' too long My fixed length record has length around 2000, each file is having 5000 records and total number of files is... (3 Replies)
Discussion started by: Devesh5683
3 Replies

5. Shell Programming and Scripting

determine the active processes on the system which are running since long time

Hi , Please help me shell script to determine the active processes on the system which are running since long time (2 Replies)
Discussion started by: itian2010
2 Replies

6. Shell Programming and Scripting

plus values from two files differient fields.

Hi Everyone, I have two files: filea: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:16 2009 2 3 4 fileb: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:17 2009 33 44 55 The output is filec: Sun Jun 21... (4 Replies)
Discussion started by: jimmy_y
4 Replies

7. Shell Programming and Scripting

plus values from two files in differient rows

Hi Guys :), I have two files. filea: 12:20:23 4 12:21:23 3 12:22:23 2 12:25:23 3 fileb: 11:20:23 4 12:21:23 3 12:22:23 2 12:26:23 3 12:30:23 3 The result will be: 11:20:23 4 12:20:23 4 (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. Shell Programming and Scripting

Split long record into csv file

Hi I receive a mainframe file which has very long records (1100 chars) with no field delimiters. I need to parse each record and output a comma delimited (csv) file. The record layout is fixed. If there weren't so many fields and records I would read the file into Excel, as a "fixed width"... (10 Replies)
Discussion started by: wvdeijk
10 Replies

9. Shell Programming and Scripting

Record Length too long -- AWK Problem

Hi All, I have a txt file which is an export of a query result from the database. The txt file contains 'Processid#sqlquery' from the database table.As the sqlquery is too long.... i am unable to get the fields seperated using the awk script as below:- cat sql.txt | awk -F'#' '{printf $2}'... (2 Replies)
Discussion started by: venkatajay_18
2 Replies

10. UNIX for Dummies Questions & Answers

Record too long for awk

I am trying to generate a small report with the help of awk. The contents are present in a file whose last line is very long. I can't shorten this line as its generated after a lot of processing. On reading this file awk says record "starting of line ..." too long record number 30 Now... (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies
Login or Register to Ask a Question