Read and respond to this message at: https://sourceforge.net/projects/gnuwin32/forums/forum/74807/topic/4550522 By: raysatiro
Reggie what you first need to understand is how your command line arguments will be seen by a gcc compiled C program after the command line is parsed. I wrote a very simple program to demonstrate this. These examples were made using the windows command interpreter. [code] C:\Temp>getargs "{if (substr($1,1,3) == "123") print $0}" argv[ 0 ]: getargs argv[ 1 ]: {if (substr($1,1,3) == 123) print $0} C:\Temp>getargs "{if (substr($1,1,3) == \"123\") print $0}" argv[ 0 ]: getargs argv[ 1 ]: {if (substr($1,1,3) == "123") print $0} C:\Temp>getargs "{if (substr($1,1,3) == "Sun") print $0}" argv[ 0 ]: getargs argv[ 1 ]: {if (substr($1,1,3) == Sun) print $0} C:\Temp>getargs "{if (substr($1,1,3) == \"Sun\") print $0}" argv[ 0 ]: getargs argv[ 1 ]: {if (substr($1,1,3) == "Sun") print $0} C:\Temp>getargs "apple "orange" pear" argv[ 0 ]: getargs argv[ 1 ]: apple orange pear C:\Temp>getargs "apple " orange " pear" argv[ 0 ]: getargs argv[ 1 ]: apple argv[ 2 ]: orange argv[ 3 ]: pear C:\Temp>getargs "apple \" orange \" pear" argv[ 0 ]: getargs argv[ 1 ]: apple " orange " pear C:\Temp>getargs "apple""orange" argv[ 0 ]: getargs argv[ 1 ]: apple"orange C:\Temp>getargs "apple "" orange" argv[ 0 ]: getargs argv[ 1 ]: apple " argv[ 2 ]: orange C:\Temp>getargs "apple" argv[ 0 ]: getargs argv[ 1 ]: apple C:\Temp>getargs \"apple\" argv[ 0 ]: getargs argv[ 1 ]: "apple" [/code] Arguments are separated by spaces and each argument can be quoted. If an argument contains spaces it must be quoted. If an argument contains special characters in most cases it should/must be quoted. If an argument contains quotes in the argument in most cases you would want to escape them. There is also "apple""orange" parsed as one argument apple"orange . But that is undocumented. If you want a more thorough understanding of command line parsing go here: http://www.autohotkey.net/~deleyd/parameters/parameters.htm#WIN Now as to your question. You can see above that what is happening is while in your first command you may have intended to compare to a numeric string of "123" you were actually comparing a numeric of 123 because you didn't escape the quotes. gawk's type comparison rules, which are the same as POSIX's, allow that. It gets kind of complicated but basically it's okay to compare what you get from substr() to a numeric. Also, while you intended to compare to a string of "Sun" you were actually comparing to a variable named Sun because you didn't escape the quotes. Unless the unquoted argument in that if statement is a numeric (+2 or 234 etc.) gawk interprets it as a variable name. In this case an undefined variable, because there is no variable named Sun... unless you define it, and why would you do that except to make things really confusing! If you feel up to it review gawk's documentation on String Type Versus Numeric Type: http://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html#Variable- Typing One of the most confusing issues for gnuwin32 users reading GNU documentation is that in many cases it assumes a POSIX compliant shell when it gives usage examples. The windows command interpreter (what most users use) is certainly not a POSIX compliant shell. So the examples in the link above you won't be able to run in a windows command interpreter due to the way they're quoted. You must use double quotes. So take this example: [code]echo ' +3.14' | gawk '{ print $1 == "+3.14" }'[/code] In the windows command interpreter you must do it like this: [code]c:\gnuwin32\bin\echo " +3.14" | gawk "{ print $1 == \"+3.14\" }"[/code] Although I focused on the windows command interpreter there are POSIX shells for windows that you can use instead of the command interpreter. If you want POSIX compliance Microsoft offers that for certain versions of windows: http://en.wikipedia.org/wiki/Windows_Services_for_UNIX Personally I alternate between the POSIX bash shell that came with my MSYS installation and the windows command interpreter. I don't use SFU. As your gawk code becomes more complex it will be easier to just move it to a script file where you won't have any of these problems, POSIX shell or not. gawk -f yourscript in.txt > out.txt The program getargs and its source is available here: https://sourceforge.net/projects/getgnuwin32/files/getgnuwin32/other/getargs/ _____________________________________________________________________________________ You are receiving this email because you elected to monitor this topic or entire forum. To stop monitoring this topic visit: https://sourceforge.net/projects/gnuwin32/forums/forum/74807/topic/4550522/unmonitor To stop monitoring this forum visit: https://sourceforge.net/projects/gnuwin32/forums/forum/74807/unmonitor ------------------------------------------------------------------------------ Simplify data backup and recovery for your virtual environment with vRanger. Installation's a snap, and flexible recovery options mean your data is safe, secure and there when you need it. Data protection magic? Nope - It's vRanger. Get your free trial download today. http://p.sf.net/sfu/quest-sfdev2dev _______________________________________________ GnuWin32-Users mailing list GnuWin32-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gnuwin32-users