OFS print awk

file:

sasa|asasa|asasa|asas
erer|Erer|rere|ererer

Output needed :

sasa:asasa:asasa:asas
erer:Erer:rere:ererer

Im getting output, when i use the $1,$2.

awk -F'|' 'BEGIN{OFS=":";} {print $1,$2; }' file

Output :

sasa:asasa
erer:Erer

But when i need the whole column, i cant

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

try

awk -F'|' 'BEGIN{OFS=":";}{$1=$1}1' file
sed 's/|/:/g' file
tr '|' ':' < file

OFS is only used when there a comma is used with the print command to separate printed values or when a value is assigned to $1 .. $NF . That is why $1=$1 in pamu's post makes a difference.

From GNU AWK manual

Understanding $0

It is important to remember that $0 is the full record, exactly as it was read from the input. This includes any leading or trailing whitespace, and the exact whitespace (or other characters) that separate the fields.

It is a not-uncommon error to try to change the field separators in a record simply by setting FS and OFS, and then expecting a plain �print� or �print $0� to print the modified record.

But this does not work, since nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as �$1 = $1�, as described earlier.