awk error - validating strings in columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk error - validating strings in columns
# 1  
Old 08-15-2014
awk error - validating strings in columns

can someone please help me fix this command:
Code:
somecommand.sh | awk -F"---" 'BEGIN{count=0} /P/ && /ERROR/ {if (($3 ~ /^P$/) && ($6 ~ /ERROR/)) {print; count++ }END { print count } ;}'

basically, what i'm attempting to do here is print all the matching lines, then, at the end, print the total number of the matching lines that were just displayed.

when i run the above, i'm getting this:
Code:
 Syntax Error The source line is 1.
 The error context is
                BEGIN{count=0} /P/ && /ERROR/ {if (($3 ~ /^P$/) && ($6 ~ /ERROR/)) >>>  END <<<  {print; count++ }END { print count } ;}
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
$

this is on a AIX box.
# 2  
Old 08-15-2014
Think your blocks were incorrect (you need to close the 2nd command block before starting an END block) try this:

Code:
awk -F"---" '
BEGIN{count=0} 
/P/ && /ERROR/ {
    if (($3 ~ /^P$/) && ($6 ~ /ERROR/)) {
        print; count++
    }
}
END { print count }'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 08-15-2014
Expanding on what Chubler_XL has already said:

I know some people think it is cool to write unreadable 1-line awk scripts. But, when awk tells you that END is invalid at that point in the script and you can't see that you have mismatched braces, it is time to reorganize your code so you can read it. If we reformat your script as:
Code:
somecommand.sh | awk -F"---" '
BEGIN {	count=0 }
/P/ && /ERROR/ {
	if (($3 ~ /^P$/) && ($6 ~ /ERROR/)) {
		print
		count++
	}
END {	print count }
;}'

It is easy to see that the first { in red should have a matching } before the END and that the code on the last line in red doesn't have a match either.

It is also much easier to see that you are looking for two strings anywhere on a line and then looking for those same two strings in specific fields. Why test each condition twice?

Would this do what you want:
Code:
somecommand.sh | awk -F"---" '
BEGIN {	count=0 }
$3 == "P" && $6 ~ /ERROR/ {
	print
	count++
}
END {	print count }'

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

(g)awk: Matching strings from one file in another file between two strings

Hello all, I can get close to what I am looking for but cannot seem to hit it exactly and was wondering if I could get your help. I have the following sample from textfile with many thousands of lines: File 1 PS001,001 HLK PS002,004 L<G PS004,002 XNN PS004,006 BVX PS004,006 ZBX=... (7 Replies)
Discussion started by: jvoot
7 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. Shell Programming and Scripting

Convert Columns in Rows delimited by a strings

Hi Gurus, I have a file that contain inventory information from someones computers: UserName domain\user1 DNSHostName machine1 Caption Microsoft Windows 7 Professional OSArchitecture 64 bits SerialNumber XXX Name HP EliteBook Revolve 810 G1 NumberOfProcessors 1 Name Intel(R)... (2 Replies)
Discussion started by: gilmore666
2 Replies

5. Shell Programming and Scripting

validating data in columns and writing them to new file

I have a file that contains records in the below format: 23857250998423948239482348239580923682396829682398094823049823948 23492780582305829852095820958293582093585823095892386293583203248 23482038509825098230958235234230502958205983958235820358205892095... (10 Replies)
Discussion started by: shellhelp
10 Replies

6. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

7. Shell Programming and Scripting

awk for validating

HI, I am trying to write a validation script as below awk '($1=="ABC"&&$2="XYZ" ,then check the value in $8,$10 these columns should not be null. so their should be some corresponding value n the $8 and $10 column,if their is no value the script has to give error saying that at a... (2 Replies)
Discussion started by: gaur.deepti
2 Replies

8. Shell Programming and Scripting

Copying of multiple columns of one table to another by mapping with particular strings.

Hi, I would like to copy some columns from a particular file by mapping with the string names. i am using the .csv file format. my one file consist of 100 of columns but i want only particular 4 columns such as ( First_name, Middle_name,Last_name & Stlc). but they are listed in many files... (15 Replies)
Discussion started by: dsh007
15 Replies

9. Shell Programming and Scripting

Match strings in two files and compare columns of both

Good Morning, I was wondering if anybody could tell me how to achieve the following, preferably with a little commenting for understanding. I have 2 files, each with multiple rows with multiple columns. I need to find each row where the value in column 1 of file 1 matches column 1... (10 Replies)
Discussion started by: GarciasMuffin
10 Replies
Login or Register to Ask a Question