I have this script that is meant to trim the field specified as argument to the script.
ie sh script.sh file.txt "|" 2
#!/bin/bash
filename="$1"
delim="$2"
arg="$3"
gsubber="\"gsub("^[ \t]*|[ \t]*$","",'\$$arg')\""
myout=`nawk -F"$delim" -v fl="$gsubber" \'{ { fl } }1\' OFS="$delim" "$filename"`
echo "$myout"
So this file ‘file.txt’ as input:
sid|storeNo|latitude
9| gerdy| fd¿kjhn422-405
0000543210 |gfdjk39
gfd|fd||fd
becomes this output:
sid|storeNo|latitude
9|gerdy| fd¿kjhn422-405
0000543210 |gfdjk39
gfd|fd||fd
I get this error:
nawk: syntax error at source line 1
context is
‘ <<<
missing }
nawk: bailing out at source line 1
Once someone can assist with providing the correct syntax, I should have no trouble extending it to support multiple fields. ie sh script.sh file.txt "|" 2 3 could then trim the 2nd and 3rd field only.
Thanks in advance!
Try:
Edit:
In order to handle multiple fields in the arguments (see comments below):