Split column after nth character

awksed

I'm trying to split my second column in the file below after every 4 characters.

file.txt

>1A    THISISATEST
>1B    THATISATEST

desired output:

>1A    THIS    ISATEST
>1B    THAT    ISATEST

After searching and attempting to modify, I tried to use this sed command: sed 's/(.{4})(.{7}).*/\2 \3/' file.txt. However, I can't seem to get it to work. Am I missing something? However, if you have an awk suggestion, that would also be helpful. Also, please explain your suggestions. I'm in the learning process of awk and sed.

Best Answer

Here is a solution with awk. It separates first four characters and rest of the 2nd column into two variables and print them.

]$ awk '{s=substr($2,1,4)}{g=substr($2,5,length($2))}{print $1,s,g}' file.txt
1A THIS ISATEST
1B THAT ISATEST
Related Question