- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- 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
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
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
03-25-2004 03:59 AM
03-25-2004 03:59 AM
awk 'BEGIN { FS="|"; OFS="|" }
{ (if NF = 4 )
{ print $0 } > good_file.txt
else
{ print $0 } > bad_file.txt
} < input_file.txt
I keep getting the cryptic awk "something wrong on line 3" message. What am I missing here?
Regards,
CB
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:05 AM
03-25-2004 04:05 AM
Re: Awk help
if (NF = 4)
I assume you meant a comparison rather than an assignment. Use "==".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:10 AM
03-25-2004 04:10 AM
Re: Awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:15 AM
03-25-2004 04:15 AM
Re: Awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:20 AM
03-25-2004 04:20 AM
Re: Awk help
Is my syntax correct for the 'if' statement?
I've tried restructuring this several ways but still no success. Obviously I haven't hit the right way.
CB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:22 AM
03-25-2004 04:22 AM
Re: Awk help
print [expression-list] [ > expression]
So you need to (double)quote file.txt to make it an expression. And you have the closing curly brace in the wrong place.
Try something like:
awk '{ if (NF==2) {print $0 >> "good_file.txt"} else {print $0 >> "bad_file.txt"}}'
aap
aap noot
aap noot mies
$ cat good_file.txt
aap noot
$ cat bad_file.txt
aap
aap noot mies
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:23 AM
03-25-2004 04:23 AM
Re: Awk help
Next (if NF = 4) should be "if (NF == 4)".
No quotes around the filename:
awk 'BEGIN { FS="|"; OFS="|" }
{
if (NF == 4 ) print $0 > "good_file.txt"
else print $0 > "bad_file.txt"
}' < input_file.txt
should be close.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:37 AM
03-25-2004 04:37 AM
Re: Awk help
# perl -aF\\\| -pe'select@F==4?STDOUT:STDERR' input_file.txt >good_file.txt 2>bad_file.txt
or
# perl -aF\\\| -ne'print{@F==4?STDOUT:STDERR}$_' input_file.txt >good_file.txt 2>bad_file.txt
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:39 AM
03-25-2004 04:39 AM
Re: Awk help
I also need to replace the file name with a variable but I'm not sure it would matter.
cb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:44 AM
03-25-2004 04:44 AM
Re: Awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 04:58 AM
03-25-2004 04:58 AM
Re: Awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 05:14 AM
03-25-2004 05:14 AM
Re: Awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 05:18 AM
03-25-2004 05:18 AM
Re: Awk help
Yes, I DO use variables....I have to.
;-)
cb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 06:16 AM
03-25-2004 06:16 AM
Re: Awk help
I decided to use nawk so I can find out what's exactly wrong here.
awk 'BEGIN { FS="|"; OFS="|" }
{ if ( NF = 12 )
{ print $0 } > ${tmp_file3}
else
{ print $0 } > ${tmp_file4}
}' < ${tmp_file2}
I get this error:
nawk: syntax error at source line 3
context is
{ print $0 } >>> > <<< ${tmp_file3}
nawk: illegal statement at source line 3
nawk: syntax error at source line 4
I also tried to put double quotes around the variable but that didn't work.
CB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 06:46 AM
03-25-2004 06:46 AM
Re: Awk help
{ print $0 } > ${tmp_file3}
else
{ print $0 } > ${tmp_file4}
should read:
{ print $0 > ${tmp_file3} }
else
{ print $0 > ${tmp_file4} }
By including the "}" before the ">", you have effectively ended the print statement.
Best regards,
KEnt M. Ostby
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 07:09 AM
03-25-2004 07:09 AM
SolutionI do not have a system available at the moment, but I think your problems derive from the use of the dollar sign, e.g.:
print $0 } > ${tmp_file3}
You cannot directly expand a shell variable in an awk script by a dollar sign, awk will not interpret thia as a shell variable.
You have to define which shell variables you want to use in your awk script, e.g.
awk -v FILE1=/tmp/flip -v FILE2=/tmp/flop 'rest of script in which you use the variables in awk without the dollar sign, e.g. print >FLIP'
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2004 07:13 AM
03-25-2004 07:13 AM
Re: Awk help
Thanks to all who helped me!
Cathy