Reputation: 11304
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!
Upvotes: 0
Views: 375
Reputation: 28000
Try:
#!/bin/bash
filename=$1
delim=$2
arg=$3
regex='^[ \t]*|[ \t]*$'
myout=$(
nawk -F"$delim" -v regex="$regex" -v arg="$arg" '
{ gsub(regex, "", $arg) }
1' OFS="$delim" "$filename"
)
printf '%s\n' "$myout"
Edit:
In order to handle multiple fields in the arguments (see comments below):
#!/bin/bash
filename=$1
delim=$2
shift 2
args=$@
regex='^[ \t]*|[ \t]*$'
myout=$(
nawk -F"$delim" -v regex="$regex" -v args="$args" '{
n = split(args, t, " ")
for (i = 0; ++i <=n;)
gsub(regex, "", $t[i])
}1' OFS="$delim" "$filename"
)
printf '%s\n' "$myout"
Upvotes: 1
Reputation: 195209
this should work:
#!/bin/bash
filename="$1"
delim="$2"
arg="$3"
myout=`nawk -F"$delim" -v f="$arg" '{gsub(/^[ \t]*|[ \t]*$/,"",$f) }1' OFS="$delim" "$filename"`
echo "$myout"
you don't have to extract gsub out, since in the gsub function call, only field index is variable. you could pass the field index as var to awk.
Upvotes: 0