Awk field delimiters after field expansion

Is it always awklost after changing a certain field in the information about the separator of output fields? What happens if there are several field separators and I want them to be restored?

For example, suppose I have a simple file examplethat contains:

a:e:i:o:u

If I just ran a awkscript that takes into account the input field separator, which prints every line in my file, like running

awk -F: '{print $0}' example

I will see the source line. If, however, I change one of the fields directly, for example. with

awk -F: '{$2=$2"!"; print $0}' example

I do not return a modified version of the original string, instead I see fields separated by default space separator, for example:

a e! i o u

I can return a modified version of the original by specifying OFS, for example:

awk -F: 'BEGIN {OFS=":"} {$2=$2"!"; print $0}' example

, , ?

, example :, ; , -F":|;" , OFS .

, example2,

a:e;i:o;u

awk -F":|;" 'BEGIN {OFS=":"} {$2=$2"!"; print $0}' example2

( -F"[:;]"),

a:e!:i:o:u

: ; ,

a:e!;i:o;u
+4
1

GNU awk 4- split(), , RT RS:

$ awk -F'[:;]' '{split($0,f,FS,s); $2=$2"!"; r=s[0]; for (i=1;i<=NF;i++) r=r $i s[i]; $0=r} 1' file
a:e!;i:o;u

FS - , , FS, , . GNU awk 4- (), , / . comp.lang.awk awk gawk, , .

. split() https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions.

+4

All Articles