Domanda

I tried the following command

ls | awk '{ sub(/.cpp/, " ", $0); print($0); }'

Result

awk: syntax error near line 1
awk: illegal statement near line 1

help me find the error

È stato utile?

Soluzione

Whenever you get the error message:

awk: syntax error near line 1
awk: illegal statement near line 1

it means you are running old broken awk. On Solaris that's very regrettably the default /bin/awk and you should use /usr/xpg4/bin/awk instead. nawk is another alternative but not as close to POSIX compliance.

Now, while your script:

ls | awk '{ sub(/.cpp/, " ", $0); print($0); }'

will execute fine with a non-broken awk, you should re-write it as:

ls | awk '{ sub(/.cpp/, " "); print }'

rather than including the default $0 arguments for those commands.

Finally - /.cpp/ is using '.' as an RE metacharacter, not a literal period character and will match .cpp the first time it sees it onthe line so if you had a file named fooxcppbar.cpp the sub() would turn it into foo bar.cpp. Is that what you want? If not, you might want this instead:

ls | awk '{ sub(/\.cpp/, " "); print }'

It'll still falsely match on .cpp earlier in the file name if present.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top