escaping backslash and bracket on windows awk - windows

I have an awk command that I want to use on cmd. The following command works well in bash, but fails on windows cmd:
echo errr | awk '/err/ { $0 = "\033[32m" $0 "\033[39m" }; 1'
I get the following error on windows:
awk: cmd. line:1: '/err/
awk: cmd. line:1: ^ invalid char ''' in expression
After going through some questions, I changed my command to:
echo errr | awk "/err/ { $0 = "\033[32m" $0 "\033[39m" }; 1"
but that gives me:
awk: cmd. line:1: /err/ { $0 = \033[32m $0 \033[39m }; 1
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: /err/ { $0 = \033[32m $0 \033[39m }; 1
awk: cmd. line:1: ^ syntax error
How can I port my command to work in cmd?

Standard advice when running awk on Windows:
a) don't do it, install cygwin and run awk from there instead
b) if "a" is not possible then create a file "foo.awk", store your script /err/ { $0 = "\033[32m" $0 "\033[39m" }; 1 in that, and then run it as awk -f foo.awk to avoid Windows nightmarish quoting rules.

instead of awk 'your commands' use awk -e 'your commands', there should be no error. I do not have windows to check. Will it be coloring the text? Read my comment below your question.
EDIT:
OK, now if you have version 6 in PowerShell, it should work coloring like this:
echo errr | awk -e "/err/ { $0 = '`e[32m' $0 '`e[39m'}; 1 "
If you have a different version windows, look for the correct escape sequence in the link I provided.

Related

Awk cross reference code not working on different platform

I have a cross reference bash script using awk, however it works on my laptop but doesn't work on my other computer :s..
script example is :
"C:\cygwin\bin\gawk.exe" -F: "FNR==NR{a[$2]=$1;next} $1 in a{print a[$1] FS $2}" username.email.txt email.phone.txt > username.phone.txt
username.email input:
example:email#email.com
email.phone.txt input:
email#email.com:0123456789
username.phone output:
example:0123456789
so what happens here is if email is in > email.phone.txt, output > username & phone in username.phone.txt cross referencing between the 3 files.
this works fine on another laptop, however doesn't work on another computer.. i get a syntax error & invalid subscript expression.
Example of error:
gawk: cmd. line:1: FNR==NR{a[]=;next} in a{print a[] FS }
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: error: invalid subscript expression
gawk: cmd. line:1: FNR==NR{a[]=;next} in a{print a[] FS }
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: FNR==NR{a[]=;next} in a{print a[] FS }
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: FNR==NR{a[]=;next} in a{print a[] FS }
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: error: invalid subscript expression
Why use double-quotes for the body of awk commands? $1 has a special meaning when double-quoted, the values of $1 are being evaluated as positional arguments and are passed to awk, since they don't have values for it, that is why they are empty. Since awk sees an empty subscript array it complains it is not a valid array.
Simply single-quote it to solve the problem.
"C:\cygwin\bin\gawk.exe" -F: 'FNR==NR{a[$2]=$1;next} $1 in a{print a[$1] FS $2}' username.email.txt email.phone.txt > username.phone.txt
In general we single-quote the action(s) part to awk to pass them as literal strings to not let the shell do its parsing before passing it to awk. As mentioned single-quotes pass the string as-is without going through any expansion.
If you still want to go through the pain of using double-quotes, escape the dollar sign to deprive of its special meaning i.e. with an escaped character the dollar variables do not go through expansion ( not recommended in any way)
"C:\cygwin\bin\gawk.exe" -F: "FNR==NR{a[\$2]=\$1;next} \$1 in a{print a[\$1] FS \$2}" username.email.txt email.phone.txt > username.phone.txt

Bash-shell awk if-else with ternary operator grammar issue

cat file
AirIfLoadProfile trafficModelPrb ulDlRatioPerQci
EUtranCellTDD servOrPrioTriggeredErabAction 1
When I execute the command:
awk '($NF!~/^[0-9]+$/)?{printf("%s,%s,%s",$1,$2.$3)}:{printf("%s,%s,%s",$1,$2,$3)}' file
It comes to the error below:
awk: cmd. line:1: ($NF!~/^[0-9]+$/)?{printf("%s,%s,%s",$1,$2.$3)}:{printf("%s,%s,%s",$1,$2,$3)}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: ($NF!~/^[0-9]+$/)?{printf("%s,%s,%s",$1,$2.$3)}:{printf("%s,%s,%s",$1,$2,$3)}
awk: cmd. line:1: ^ syntax error
Please help me find the format issue.
The right way:
awk '{ printf("%s %s%s%s\n",$1,$2,($NF~/^[0-9]+$/? " ":"."),$3) }' file
the 3rd format specifier %s accepts the result of the condition ($NF~/^[0-9]+$/? " ":".")
The output:
AirIfLoadProfile trafficModelPrb.ulDlRatioPerQci
EUtranCellTDD servOrPrioTriggeredErabAction 1

Awk a space-separated file

When I awk the following, an error is encountered.
awk -F '$1' "2.\ 2006-07\ and\ 2007-08\ ERB\ IN.csv"
Here is the error:
awk: cmd. line:1: 2.\ 2006-07\ and\ 2007-08\ ERB\ IN.csv
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: 2.\ 2006-07\ and\ 2007-08\ ERB\ IN.csv
awk: cmd. line:1: ^ syntax error
If you want to print the first field in each line of the file, it should be:
awk '{print $1}' "2. 2006-07 and 2007-08 ERB IN.csv"
Since you left out the script argument to awk, it treated "2.\ 2006-07\ and\ 2007-08\ ERB\ IN.csv" as the script to execute. But that filename is not valid awk script syntax.

AWK syntax error - what's causing it?

I have simple bash script:
#!/bin/sh
column=${1:-1}
awk ' {colawk='$column'+2; print $colawk}'
awk '(x=4; print $x)'
But I have received error:
awk: (x=4; print $x)
awk: ^ syntax error
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1: ^ unexpected newline or end of string
Why? Code in the previous line works.
An AWK program is a series of pattern action pairs, written as:
condition { action }
where condition is typically an expression and action is a series of commands.
print is not expression but a statement, so it's a syntax error as expected.
Your problem is with using parentheses instead of braces. Try:
awk '{x=4; print $x}'
instead, as in the following transcript:
pax$ echo a b c d e | awk '(x=4; print $x)'
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (x=4; print $x)
awk: cmd. line:2: ^ unexpected newline or end of string
pax$ echo a b c d e | awk '{x=4; print $x}'
d

Awk - unterminated regex

I am writing a shell script which needs to pull values out of a text file which looks like this:
app.full.name /warfilelocation/ warfilename
My shell script will be iterating over a list of application names and pulling out either the location or name using AWK. I have tested doing this on the command line using the following:
awk "\$1 ~/app.full.name/ { print $2 }" applications.txt
which returns what I would expect however when i put this in a shell script I start having issues.
I have a function that looks like this:
function get_location() {
local application=$1
awk "\$1 ~/^$application/ { print \$2 }" applications.txt
}
But when i call this function i get the following error:
awk: $1 ~/^app.full.name
awk: ^ unterminated regexp
awk: cmd. line:1: app.full.name
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: app.full.name/ { print $2 }
awk: cmd. line:2: ^ syntax error
Does anyone have any ideas what I am doing wrong here. I presume I am not escaping the variable correct but no matter what i try it doesnt seem to work.
Thanks in advance
Use this approach to make awk recognize shell variables:
awk -v "v1=$VAR1" -v "v2=$VAR2" '{print v1, v2}' input_file
Update
$ cat input
tinky-winky
dipsy
laa-laa
noo-noo
po
$ teletubby='po'
$ awk -v "regexp=$teletubby" '$0 ~ regexp' input
po
Note that anything could go into the shell-variable,
even a full-blown regexp, e.g ^d.*y. Just make sure to use single-quotes
to prevent the shell from doing any expansion.
The error messages seem to indicate that there is a stray newline at the end of $application, which gives the "line 2" error messages.
see this: using awk match() function
kent$ app=app.ful
kent$ echo "app.full.name /warfilelocation/ warfilename"|awk -v a=$app '{if(match($1,a))print $2}'
/warfilelocation/
It's hard to tell without knowing exactly the value of $application, but it seems like you have a strange character in $application, such as a " or a / or something like that.
$ export application=foo/bar
$ awk "\$1 ~/^$application/ { print \$1 }"
gawk: cmd. line:1: $1 ~/^foo/bar/ { print $1 }
gawk: cmd. line:1: ^ parse error
I would look at the exact value that you have in $application, and if it contains a /, escape it.
One way to do this would be to use:
$ export application=`echo foo/bar | sed -e 's;/;\\\\/;g'`
$ awk "\$1 ~/^$application/ { print \$1 }"

Resources