Suppose $2 is my variable. I have tried going from
awk -F\, '{print $2 ":"}'
to
awk -F\, '{print gsub(/[ \t]+$/, "", $2) ":"}'
But it goes from printing something to printing nothing at all.
${var/ /} removes the first space character. ${var// /} removes all space characters. There's no way to trim just leading and trailing whitespace with only this construct.
You can cut in awk using awk's function split . You can also filter records using a regex condition within awk, making grep and cut superfluous. This breaks down like: Set the output field separator to be a pipe between two spaces.
You're printing the result of the gsub
, but gsub
does an in-place modify of $2
instead of returning a modified copy. Call gsub
, then print:
awk -F\, '{gsub(/[ \t]+$/, "", $2); print $2 ":"}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With