Insert space in specific column among many columns

Hello,

I have some problem in inserting the space for the pairs of columns.

I have the input file :

I used this code below in replacing it using space in specific column (replace space in each two columns)

sed -e "s/,/ /2" -e "s/,/ /3" inputfile 

Output showed :

However, I have many columns around 17,000 columns. I do not know how to use loop in this command. Could you please suggest me?

Thanks in advance :slight_smile:

sed have no loop command, use awk

awk -F, '{for (i=1;i<=NF;i+=2) printf "%s,%s ",$i,$(i+1);print ""}' inputfile
A,A B,C D,R
C,R A,A B,R
R,D A,R B,R
1 Like

Great. It is worked. Thank you very much Jotne.

You are mistaken. Explicit looping can be implemented with labels ( : ) and either or both of the branching (b) and testing (t) commands. However, a sed solution does not require any explicit looping, only a single substitution command:

sed 's/\([^,]*,[^,]*\),/\1 /g'

N.B. The sed solution never alters the number of fields in a record. That AWK solution will add a comma and an empty field if there are an odd number of fields (i.e. an even number of commas).

Regards,
Alister

1 Like