Convert a script in awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert a script in awk script
# 1  
Old 06-08-2005
Error Convert a script in awk script

Hello guys,

I have a script like:

echo "Errores 0x01 `cat errores.log | grep 0x00000001 | wc -l` " > class_total
echo "Errores 0x0B `cat errores.log | grep 0x0000000B | wc -l` " >> class_total
echo "Errores 0x45 `cat errores.log | grep 0x00000045 | wc -l` " >> class_total
echo "Errores 0x58 `cat errores.log | grep 0x00000058 | wc -l` " >> class_total
echo "Errores 0x64 `cat errores.log | grep 0x00000064 | wc -l` " >> class_total
echo "Errores 0x66 `cat errores.log | grep 0x00000066 | wc -l` " >> class_total
echo "T O T A L `cat errores.log | grep 0x000 | wc -l` " >> class_total

and when i run it i get a 'class_total' file like:

Errores 0x01 188
Errores 0x0B 127
Errores 0x45 0
Errores 0x58 0
Errores 0x64 38
Errores 0x66 140
T O T A L 493

but the file 'errores.log' that i im counting have 5'000.000 of lines and this process take a few seconds, how i do the same in awk to improve the time of response?
# 2  
Old 06-08-2005
nawk -f lest.awk errors.log

here's lest.awk - not tested
Code:
BEGIN {
  codesN=split("0x00000001 0x0000000B 0x00000045 0x00000058 0x00000064 0x00000066", codesA, " ");
  split("0x01 0x0B 0x45 0x58 0x64 0x66", codesAs, " ");
}

{
   for(i=1; i <= codesN; i++)
     if ( $0 ~ codesA[i] )
        arr[codesAs[i]]++
}

END {
  for( i in arr ) {
    printf("Errores %s %d\n", i, arr[i])
    tot+=arr[i]
  }
  printf("TOTAL %d\n", tot)
}

# 3  
Old 06-09-2005
I don't know too much of awk, i just know that awk is powerfull as another, when i run the following script u have this problem:

awk: syntax error near line 1
awk: bailing out near line 1

Quote:
Originally Posted by vgersh99
nawk -f lest.awk errors.log

Code:
BEGIN {
  codesN=split("0x00000001 0x0000000B 0x00000045 0x00000058 0x00000064 0x00000066", codesA, " ");
  split("0x01 0x0B 0x45 0x58 0x64 0x66", codesAs, " ");
}

{
   for(i=1; i <= codesN; i++)
     if ( $0 ~ codesA[i] )
        arr[codesAs[i]]++
}

END {
  for( i in arr ) {
    printf("Errores %s %d\n", i, arr[i])
    tot+=arr[i]
  }
  printf("TOTAL %d\n", tot)
}

what can i do?
# 4  
Old 06-09-2005
use nawk instead of awk.
# 5  
Old 06-09-2005
Not yet, i still have problems:

example# nawk prueba.awk borrar

nawk: syntax error at source line 1
context is
>>> prueba. <<< awk
nawk: bailing out at source line 1
# 6  
Old 06-09-2005
vgersh99's solution is good, but it might be possible to refine it a bit. Assuming a nawk/gawk (likewise untested):
Code:
k = match($0,/0x000000(01|0B|45|58|64|66)/) {
    arr[substr($0, k + 8, 2)]++
}

END {
    for (i in arr)
        print "Errores", "0x" i, arr[i]
}

What are the differences?
  • Direct match against all the patterns you seek at one time, i.e., no iteration over codesA on every line
  • No lookup in codesAs for each item found

[Old awk solution deleted --- no alternation ("|") available.]

The disadvantage here is that nawk and awk don't "capture" the thing matched, so you still do the substr call on every match (though it's in machine language and therefore fast). (I don't use gawk, but it may have a remedy for this.)

A ruby solution would work like this (and the perl solution would be similar):
Code:
$hsh = { "01" => 0, "0B" => 0, "45" => 0, "58" => 0, "64" => 0, "66" => 0 }
ARGF.each do |line|
    next unless m = %r/0x000000(01|0B|45|58|64|66)/.match line
    $hsh[m[1]] += 1
end
$hsh.keys.sort do |k|
    print "Errores 0x", k, " ", $hsh[k]
end


Last edited by criglerj; 06-09-2005 at 02:36 PM.. Reason: Old awk solution doesn't work
# 7  
Old 06-09-2005
pay more attention to the postings.......
nawk -f prueba.awk borrar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert XML to CSV using awk or shell script

Hello, I am working on a part of code where I need a awk or shell script to convert the given XML file to CSV or TXT file. There are multiple xml files and of different structure, so a single script is required for converting data. I did find a lot of solutions in the forum but... (16 Replies)
Discussion started by: Rashmitha
16 Replies

2. Shell Programming and Scripting

Help with convert awk script into perl

Input file (a list of input file name with *.txt extension): campus.com_icmp_ping_alive.txt data_local_cd_httpd.txt data_local_cd.txt new_local_cd_mysql.txt new_local_cd_nagios_content.txt Desired output file: data local_cd_httpd data local_cd new local_cd_mysql new ... (9 Replies)
Discussion started by: perl_beginner
9 Replies

3. Shell Programming and Scripting

Help: How to convert this bash+awk script in awk script only?

This is the final first release of the dynamic menu generator for pekwm (WM). #!/bin/bash function param_val { awk "/^${1}=/{gsub(/^${1}="'/,""); print; exit}' $2 } echo "Dynamic {" for CF in `ls -c1 /usr/share/applications/*.desktop` do name=$(param_val Name $CF) ... (3 Replies)
Discussion started by: alexscript
3 Replies

4. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

5. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

6. Shell Programming and Scripting

Help: how to convert perl script to awk in windows2003 server environment

For the following perl script, can anyone help to convert it to awk statement in windows2003 server environment ? Code: foreach $k (sort {$a <=> $b} keys %psnum) (1 Reply)
Discussion started by: tojzz
1 Replies

7. Shell Programming and Scripting

Awk script to convert csv to html

Hi Written some script to convert csv to html but could not add table headers.Below are the errors iam getting ./csv2html | more + awk -v border=1 -v width=10 -v bgcolor=black -v fgcolor=white BEGIN { printf("<table border=\"%d\" bordercolor=\"%s\" width=\"%d\"... (2 Replies)
Discussion started by: zeebala1981
2 Replies

8. Shell Programming and Scripting

convert this into csv using awk/shell script

Hi Scripting gurus, I need to convert following text snippet into csv. please help Input heading1 = data1 heading2 = data2 .. .. heading n = data n heading 1 = data1 .. .. Output data1,data2,....,data n (3 Replies)
Discussion started by: azs0309
3 Replies

9. Shell Programming and Scripting

how to convert unix .ksh script to windows .batch script

I am using awk in my .ksh script but when I am trying to run in windows its not recognising awk part of the ksh script , even when I changed it to gawk it does not work, this is how my .ksh and .bat files look like. thanx. #!/bin/ksh egrep -v "Rpt 038|PM$|Parameters:|Begin |Date: |End... (1 Reply)
Discussion started by: 2.5lt V8
1 Replies

10. Shell Programming and Scripting

here-doc convert 2 script convert to single script?

I have my main script calling another script to retrive a "ls -alt" of a directory that's located in a remote location I'm sftping into. main.sh #!/bin/ksh getLS.sh > output.txt getLS.sh #!/bin/sh /home<..>/sftp <host@ip> <<! cd /some/dir/Log ls -alt quit ! Basically I'd like to be... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question