1822525 Members
2666 Online
109642 Solutions
New Discussion юеВ

Re: awk help !!

 
Tarek_1
Frequent Advisor

awk help !!

Hi there,
i'm going crazy in creating a small script in awk.
What i want to do is if in a field i have M replace value with 1 else with 0.
This is my script
BEGIN { FS="|" ; OFS="|"}
{ if ($29=="M") { $29=1 }
[ else $29=0 ]
print $0
}
I get error when i issue:
cat file | awk -f script.awk | moreSyntax Error The source line is 3.
The error context is
>>> [ <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 3.

but in the man there's written:if ( Expression ) { Statement } [ else Action ]
so what's wrong?
any help is appreciated.
Thanks
tarek

7 REPLIES 7
John Poff
Honored Contributor

Re: awk help !!

Hi,

I think you might be able to do it like this:

{$29=($29 == "M") ? "1" : "0"
print $0
}


This sets the value of $29 according to the test of $29 equal to "M". If the test is true, $29 is set to "1" else it is set to "0".

Here is a one line example where I change the password field in /etc/passwd to an "X" if the second field isn't equal to an "*", otherwise it is set to "*":

awk -F: '{$2=($2 == "*") ? $2 : "X"; print $0}' /etc/passwd


I hope that helps a bit.

JP
Tarek_1
Frequent Advisor

Re: awk help !!

John,
maybe you're script is write but i think with me it isn't functioning (no errors are given but the output is wrong) because i haven't specified the field separtor.
My file is like this:
aa|aa|ds|a;b;c;|sa|as|af
for me these are 7 fields because the separtor is | .
i have to specify it.
i issued this command:
awk -F: '{$29=($29 == "M") ? "1" : "2"; print $0}' filename

How can i specify the FS?
However thanks very much for your precious help.

Tarek
John Poff
Honored Contributor

Re: awk help !!

Hi Tarek,

The -F option specifies the field separator, so for your example try this:

awk -F| '{$29=($29 == "M") ? "1" : "2"; print $0}' filename


JP
Tarek_1
Frequent Advisor

Re: awk help !!

Again, not working:
awk -F| '{$29=($29 == "M") ? "1" : "2"; print $0}' filename
awk: A flag requires a parameter: F
Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
ksh: {$29=($29 == "M") ? "1" : "2"; print $0}: not found.

also tried with -F | , same stuff.
if i put -F "|" it works but not fine:
i get all $29=2 and in the output file the field separator is space instead of |
Ron Kinner
Honored Contributor

Re: awk help !!


Suspect you need a backslash before your | or some quotes otherwise the shell thinks you want to pipe the rest somewhere. Try:

awk -F \| '{$29=($29 == "M") ? "1" : "2"; print $0}' filename

Also in your first post you misunderstood the use of the square brackets. These are conventionally used in a programming description to indicate that the stuff in the brackets is optional. They do not appear in real life. I suspect the curly brackets were the same thing.

Aren't we supposed to use gawk in Linux?

Ron

John Poff
Honored Contributor

Re: awk help !!

Hi again,

Ron is right. I wasn't thinking about the vertical bar being the pipe symbol. Escaping it should make your line work. I tried it here and it seemed to work fine.

Ron, On my RedHat 7.2 box the /bin/awk file is a symlink to gawk, so I guess I am using gawk.

JP
Procnus
Frequent Advisor

Re: awk help !!

I may be wrong, but in the man pages anything within [..] means optional. If you are using that optional item the [ and ] are not included, you need to the line:
[ else $29=0 ]

into
else { $29=0 }

Cheers
Steven