One way to use awk . Pass two script arguments, the column number and the value to insert. The script increases the number of fields ( NF ) and goes through the last to the specified position and inserts a new value there.
Run this command:
awk -v column=4 -v value="four" ' BEGIN { FS = OFS = "|"; } { for ( i = NF + 1; i > column; i-- ) { $i = $(i-1); } $i = value; print $0; } ' 1.txt
With the following output:
1|2|3|four|5 1|2|3|four|5 1|2|3|four|5 1|2|3|four|5
source share