- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: awk help !!
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 02:28 AM
тАО01-17-2003 02:28 AM
awk help !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 07:46 AM
тАО01-17-2003 07:46 AM
Re: awk help !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 08:23 AM
тАО01-17-2003 08:23 AM
Re: awk help !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 08:35 AM
тАО01-17-2003 08:35 AM
Re: awk help !!
The -F option specifies the field separator, so for your example try this:
awk -F| '{$29=($29 == "M") ? "1" : "2"; print $0}' filename
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 08:53 AM
тАО01-17-2003 08:53 AM
Re: awk help !!
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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 09:59 AM
тАО01-17-2003 09:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2003 10:52 AM
тАО01-17-2003 10:52 AM
Re: awk help !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2003 12:03 PM
тАО01-19-2003 12:03 PM
Re: awk help !!
[ else $29=0 ]
into
else { $29=0 }
Cheers
Steven