Assign regular expression change to only first two columns

input

1_2  2_3  4_4  4_5
2_2  4_5  4_4  5_5

output

1  2  2  3  4_4  4_5
2  2  4  5  4_4  5_5

I used the following command but it changes every column. i just want to change first two.

tr '_' '\t'
awk '{sub(/_/,FS,$1);sub(/_/,FS,$2)}1' file
1 Like
sed 's/_/ /;s/_/ /' file
1 Like

Your solution apply for the first two occurrences not

you can try this also -

cat <file> | sed 's/_/ /1'| sed 's/_/ /1'

If any "column" has more than one occurrence then your solution would fail also, given the expected output. The FS is probably useful, though :slight_smile:

No need for cat, and using 1 is the same as not using g, which is the same as Scrutinizer already posted.

That's correct , let's try

awk '{gsub(/_/,FS,$1);gsub(/_/,FS,$2)}1' file

This following two solution will fail if first OR second OR both target column's don have the special char OR the target column's have more that one occurrence of special char.

sed 's/_/ /1'| sed 's/_/ /1' file
sed 's/_/ /;s/_/ /' file

If that is the case then you are correct, With the OP's example all solutions will work..