awk sed perl??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sed perl??
# 8  
Old 10-28-2011
Statistics - Executed on a file with ~68000 lines of size 1.1 MB
Intel(R) Core(TM)2 CPU 6300 @ 1.86GHz

Code:
[root@bt /tmp]ls -lrt inputfile
-rw-r--r-- 1 orange orange 1.1M Oct 28 16:56 inputfile
 
[root@bt /tmp]time uniq -c -w8 inputfile >/dev/null
real    0m0.216s
user    0m0.216s
sys     0m0.000s
 
[root@bt /tmp]time awk '{counts[$1]++} END {for (i in counts) {print i, counts[i]}}' inputfile >/dev/null
real    0m0.030s
user    0m0.028s
sys     0m0.004s
 
[root@bt /tmp]time awk '{print $1|"sort|uniq -c"}' inputfile >/dev/null
real    0m1.622s
user    0m1.264s
sys     0m0.312s
 
[root@bt /tmp]time perl -lane '$a{$F[0]}++}{foreach (keys %a){print "$_  $a{$_}"}' inputfile >/dev/null
real    0m0.198s
user    0m0.196s
sys     0m0.004s
 
#Winner
[root@bt /tmp]time awk '{counts[$1]++} END {for (i in counts) {print i, counts[i]}}' inputfile >/dev/null
real    0m0.030s
user    0m0.028s
sys     0m0.004s

--ahamed
# 9  
Old 10-28-2011
If you want to preserve the original order without using the 'sort' (which is eating your CPU cycles):
Code:
awk '!($1 in c) {key[++key[0]]=$1}{c[$1]++} END {for (i=1;i<=key[0];i++) print key[i], c[key[i]]}' myFile

# 10  
Old 10-28-2011
I was surprised uniq without sort wasn't fastest, to be honest. I always thought it would be more optimised for doing this kind of thing.
# 11  
Old 10-28-2011
Wow!! thanks guys. Thanks to all of you.
# 12  
Old 10-28-2011
ahamed101,
could you take a timing snapshot of my implementation in post #9 - just curious how much more cycles it takes than the other awk solutions.
Thanks!
# 13  
Old 10-28-2011
Code:
[root@bt /tmp]time awk '!($1 in c) {key[++key[0]]=$1}{c[$1]++} 
END {for (i=1;i<=key[0];i++) print key[i], c[key[i]]}' inputfile >/dev/null
real    0m0.046s
user    0m0.044s
sys     0m0.000s

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 14  
Old 10-28-2011
imho on such timeframes (0.2s) it's all about 'loading into memory' progress
perl is much more heavy than awk and require more time to load in memory
tip78
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace my perl with awk or sed

My code below will print only the email address from all lines. I want to convert it with sed or awk.. also what if i just want to find only filenames. cat LIS_EMAIL | perl -wne'while(/+@+\w+/g){print "$&\n"}' Hoping to extract the filename such us .exe, .bin. From file that has scrambled... (8 Replies)
Discussion started by: invinzin21
8 Replies

2. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

3. Shell Programming and Scripting

Modify the file with awk,sed or perl

Hi All, I need help from any of you.Would be so thankful for your help. I/P DDDD,1045,161,1557,429,1694,800,1911,1113,2460,1457,2917> 1609,3113,1869,3317,2732,3701,3727,4132,5857,5107> 9004,6496 DDDD,1125,157,1558,429,1694,800,1911,1117,2432,1444,2906>... (2 Replies)
Discussion started by: Indra2011
2 Replies

4. Shell Programming and Scripting

SED/AWK maybe PERL in a WHILE loop

Hi, I have a file containing numerous string values, for example: AMFU8849636 CCLU7120334 GESU5784065 TEMU3070096I have a second file with multiple records, such as the one below that I would like to manipulate based on the strings from the first file: CATOS2EDI DSTOW ABC ... (7 Replies)
Discussion started by: doza22
7 Replies

5. Shell Programming and Scripting

Advance search using sed/awk/perl

Hi, I have a file with more than 50,000 lines of records and each record is 50 bytes in length. I need to search every record in this file between positions 11-19 (9 bytes) and 32-40 (9 bytes) and in case any of the above 2 fields is alpha-numeric, i need to replace the whole 9 bytes of that... (7 Replies)
Discussion started by: kikionline
7 Replies

6. Shell Programming and Scripting

Deleting characters with sed,perl,awk

Input: :: gstreamer :: xine-lib :: xine-lib-extras Output should be: gstreamer xine-lib xine-lib-extras How can it be done with sed or perl? (12 Replies)
Discussion started by: cola
12 Replies

7. Shell Programming and Scripting

Deleting the first column with sed,awk or perl

336 brtr 256 hello Output: brtr hello How can i do this with sed,awk or perl? (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

Getting the first word using sed,awk or perl

Input: root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mail:x:8: Output: root daemon bin sys adm tty (8 Replies)
Discussion started by: cola
8 Replies

9. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

10. Shell Programming and Scripting

Question related to sed or awk or perl

Hi All, I have a big file of 100,000 lines with the following format: 1,736870,736870,1,2,5,547,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445 1,881246,881246,1,2,6,402,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445... (4 Replies)
Discussion started by: toms
4 Replies
Login or Register to Ask a Question