Awk Syntax Error "!~"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Syntax Error "!~"
# 1  
Old 06-30-2005
Awk Syntax Error "!~"

This is my Awk command.

Code:
direct_count=`awk -F; 'BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }' ls.txt`

I get a syntax error.

Code:
BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }:  not found
expr: syntax error

I'm trying to say "if the 16th field or 18th field does not contain "PBLON" or "PBNY" then increment the counter. Also, the file I'm reading from has semi-colin ";" delimiters which I specified in the -F; flag.

Also, I don't think this matters but, the csv file I'm reading from has one header line which simply contains the field names separated by semi-colins which shouldn't have caused the error.
# 2  
Old 06-30-2005
hmm i think i fixed it

I think I fixed it.

The problem was, the semi-colin is a special character in the Unix shell, so I had to simply escape it.

-F\;
# 3  
Old 06-30-2005
ack

woops... the syntax error is fixed but the awk didn't increment the way I thought it would... Will update this post in a little while after I try to figure it out myself first.
# 4  
Old 06-30-2005
Here's the problem.

Here's the problem..

In my test csv file, there are 195 records, not including the single line header.
My result for that awk gave me a value of "588" so it incremented incorrectly..
The result should be equal to or less than the total number of records.

Let me try to put it into words again and maybe someone could suggest modifications to my pattern search..

"Increment the counter if field 16 AND field 18 do NOT contain the strings "PBLON" or "PBNY".

'BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }'

I'm doing something wrong..
# 5  
Old 06-30-2005
Quote:
Originally Posted by yongho
Here's the problem..
..... snipped...
"Increment the counter if field 16 AND field 18 do NOT contain the strings "PBLON" or "PBNY".

'BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }'

I'm doing something wrong..
see the problem?

btw you don't need to initialize variables in awk - there implicitely initialized to 0 [or empty string]
# 6  
Old 06-30-2005
yes

yes thank you.. my goodness...
i have a spiltting headache today
my physician accidently damaged some of my ear canal tissue

thank you again.

Actually, I initiailze the variables because I don't want to have that empty string. In the past it caused some `expr ...` errors because I was trying to add a number with an empty string.
# 7  
Old 07-01-2005
Quote:
Originally Posted by yongho
yes thank you.. my goodness...
i have a spiltting headache today
my physician accidently damaged some of my ear canal tissue

thank you again.

Actually, I initiailze the variables because I don't want to have that empty string. In the past it caused some `expr ...` errors because I was trying to add a number with an empty string.
... or you could: [... END {printf("%d\n", direct_count)}]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

Keep getting error "-bash: ./.profile_z2: line 52: syntax error: unexpected end of file"

#!/bin/bash #-------------------------------------------------------- # Setup prompt # Author Zeeshan Mirza # Data: 06-08-2017 #-------------------------------------------------------- if then . ./.profile_custom_pre fi umask 022 set -o vi export EDITOR=vi export VISUAL=vi... (3 Replies)
Discussion started by: getzeeshan
3 Replies

2. Shell Programming and Scripting

Help with FTP Script which is causing "syntax error: unexpected end of file" Error

Hi All, Please hav a look at the below peice of script and let me know if there are any syntax errors. i found that the below peice of Script is causing issue. when i use SFTP its working fine, but there is a demand to use FTP only. please find below code and explain if anything is wrong... (1 Reply)
Discussion started by: mahi_mayu069
1 Replies

3. UNIX for Dummies Questions & Answers

awk: syntax for "if (array doesn't contain a particular index)"

Hi! Let's say I would like to convert "1", "2", "3" to "a", "b", "c" respectively. But if a record contains other number then return "X". input: 1 2 3 4 output: a b c X What is the syntax for: if(array doesn't contain a particular index){ then print the value "X" instead} (12 Replies)
Discussion started by: beca123456
12 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

Bash (Ubuntu server): Syntax error: "|" unexpected in While-loop

Hello forum, I hope my problem is easy to solve for someone in here! My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I'm only able to run it on one account at a time. After... (3 Replies)
Discussion started by: primaxx
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

"syntax error at line 21 :'done' unexpected." error message"

I am trying to run the script bellow but its given me "syntax error at line 20 :'done' unexpected." error message" can someone check to see if the script is ok? and correct me pls. Today is my first day with scripting. Gurus should pls help out #!/bin/ksh # Purpose: Check to see if file... (3 Replies)
Discussion started by: ibroxy
3 Replies
Login or Register to Ask a Question