awk malfunction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk malfunction
# 1  
Old 05-15-2016
awk malfunction

can someone help me debug why this isn't working:

Code:
awk 'NR>1 {
                                                                a["C00"] += gsub("(^| )C00( |$)", "&")
                                                        } END {
                                                                for (i in a)
                                                                        printf("%s=%s\n", i, a[i])
                                                        }' test.log

test.log:

Code:
prudb05,C000464262,T7845491
prudb06,C000464632,T784931
prubb01,C0004634632,T783595
prusb02,C000461462,T7859665

when i run it, it reports wrong numbers. its always zero. I want it to tell me how many lines it found containing the string "C00" (that's C zero zero not the letter O).

the output should be something like this:

Code:
C00=3

System this must be able to run on: all unix systems - linux, sunos, hpux, aix.
# 2  
Old 05-15-2016
Hello SkySmart,

Could you please try following and letme know if this helps you then.(I haven't tested it though.)
Code:
awk '{count=gsub("COO",X,$0);print "Line " NR " contains " count " number of occurances of COO"}' Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-15-2016
Code:
awk '/C00/ {++c} END{print "C00="c}' test.log

This User Gave Thanks to Aia For This Post:
# 4  
Old 05-15-2016
You built a nice, complex regex that matches "C00" surrounded by spaces OR at EOL OR at BOL. None of these conditions are TRUE for your file, so no surprise you get a zero count. Try modifying either your regex or your input file.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-15-2016
Aia's nice and simple approach works well for single occurrences of "C00" per line, while the gsub will count all "C00" s in a line.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-15-2016
Quote:
Originally Posted by SkySmart
can someone help me debug why this isn't working:

Code:
awk 'NR>1 {
                                                                a["C00"] += gsub("(^| )C00( |$)", "&")
                                                        } END {
                                                                for (i in a)
                                                                        printf("%s=%s\n", i, a[i])
                                                        }' test.log

test.log:

Code:
prudb05,C000464262,T7845491
prudb06,C000464632,T784931
prubb01,C0004634632,T783595
prusb02,C000461462,T7859665

when i run it, it reports wrong numbers. its always zero. I want it to tell me how many lines it found containing the string "C00" (that's C zero zero not the letter O).

the output should be something like this:

Code:
C00=3

System this must be able to run on: all unix systems - linux, sunos, hpux, aix.
Your code is looking for the number of times C00 preceded by the start of the line or a <space> and followed by a <space> or the end of the line appears on lines after the 1st line in a file named test.log. (And, those conditions are not met on any line in your sample data.)

You didn't say anything about skipping the first input line in your description, but unless you skip one of the input lines, the number of lines containing the string C00 should be 4; not 3.

Since you're using an array in your code, I will make the (hopefully not too wild) assumption that you also want to search for other strings on the same invocation of your script.

To meet your stated requirement: "tell me how many lines it found containing the string C00" and expand that to also tell you how many lines it found containing the string 463 (note how many lines contain; not how many times does the string appear), you could try something more like:
Code:
printf '%s\n' "C00" "463" | awk '
FNR == NR {
	string[++cnt] = $0
	next
}
{	for(i = 1; i <= cnt; i++)
		if($0 ~ string[i])
			a[string[i]]++
}
END {	for(i = 1; i <= cnt; i++)
		printf("%s=%d\n", string[i], a[string[i]])
}' - test.log

Of course, instead of using printf and reading the list of strings into your script from standard input, you could define the strings in another file or hardcode them into a BEGIN clause in an awk script.

With the sample data you provided in a file named test.log, the above script produces the output:
Code:
C00=4
463=2

If your requirements are not what you stated before and you can't figure out how to modify the code above to get what you need, please restate your requirements clearly and provide sample input and output that match your requirements.

The same awk code should work on any recent system that has awk, but on a Solaris/SunOS system, you'll need to use /usr/xpg4/bin/awk or nawk instead of the default awk.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-15-2016
so the command i posted was being used to search for multiple patterns in big files. it's suppose to be more efficient. and it was working up until recently. i'm trying to figure out what went wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

7. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

9. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

10. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies
Login or Register to Ask a Question