lukeaw lukeaw - 11 months ago 31
Bash Question

Finding and replacing the last space at or before nth character works with sed but not awk, what am I doing wrong?

I have a string in a test.csv file like this: "here is my string" (without quotes) and when I use sed it works just as I expect.

cat test.csv | sed -r 's/^(.{1,9}) /\1,/g'
here is,my string

Then when I use awk it doesn't work and I'm not sure why, yes I'm a rookie.

cat test.csv | awk '{gsub(/^(.{1,9}) /,","); print}'
here is my string

I need to use awk because once I get this figured out I will be selecting only one column to split into two columns with the added comma. I'm using extended regex with sed, "-r" and was wondering how or if it's supported with awk, but I don't know if that really is the problem or not.

Thanks for the help, I'm sure it's simple and I'm just missing something obvious!



awk does not support back references in gsub. If you are on GNU awk, then gensub can be used to do what you need.

echo "here is my string" | awk '{print gensub(/^(.{1,9}) /,"\\1,","G")}'
here is,my string

Note the use of double \ inside the quoted replacement part. You can read more about gensub here.