Use sed to add comma to end of first field

Example data

Gi1/10  
Gi1/12  	xl32lytscb07
3/11 		to nyc
3/12 		41764 ecomm
3/13 		hxcsxsa 2/1
3/14 		ziim570-rsvd
3/15 		xl3NDSADM
Po1            VPC trunk to xl3-i
Po2            ***DO NOT ENABLE**
Po13           *** VPC link to
Po101          
Po102          xl3-2lyg1accsgh-fe
Po200          
Po201          09-tietie22834
Po202          16-tietie22834
Po205          
Po206          19-tietie22834
mgmt0          OOB Management
Eth101/1/1     08-tietie2283
Eth101/1/2     08-tietie2283
Eth101/1/3     
Eth101/1/4     08-tietie2283
Eth101/1/5     08-tietie2283
Eth101/1/6     
Eth101/1/7     16-tietie2283
Eth101/1/8     16-tietie2283
Eth101/1/9     16-tietie2283

Desired Result

Gi1/10,  
Gi1/12,  	xl32lytscb07
3/11, 		to nyc
3/12, 		41764 ecomm
3/13, 		hxcsxsa 2/1
3/14, 		ziim570-rsvd
3/15, 		xl3NDSADM
Po1,            VPC trunk to xl3-i
Po2,           ***DO NOT ENABLE**
Po13,           *** VPC link to
Po101,          
Po102,          xl3-2lyg1accsgh-fe
Po200,          
Po201,          09-tietie22834
Po202,          16-tietie22834
Po205,          
Po206,          19-tietie22834
mgmt0,          OOB Management
Eth101/1/1,     08-tietie2283
Eth101/1/2,     08-tietie2283
Eth101/1/3,     
Eth101/1/4,     08-tietie2283
Eth101/1/5,     08-tietie2283
Eth101/1/6,     
Eth101/1/7,     16-tietie2283
Eth101/1/8,     16-tietie2283
Eth101/1/9,    16-tietie2283

Code that Ive tried

sed 's/\/[0-9]\/[0-9]*/&,/g' < $x.v >$x.vl

You can use awk instead:-

awk ' { $1 = $1","; print; } ' input_file
1 Like

You can also do that slightly shorter with:

$ awk '$1=$1","' file

but neither will maintain the spacing between fields. Perhaps using awk's sub function would be the answer.

This seems a bit yuck, but:

$ sed "s/ /,&/;/ /!s/$/,/" file

(I'm sure that can be done in one expression!)

1 Like
sed 's/^[^ ]*/&,/' file
2 Likes

Yay! That was the one of course :slight_smile:

Thanks for the solutions guys. I was trying to figure this out for 2 days.

Depending on the delimiter, you may want to modify slightly:

sed 's/^[^ \t]*/&,/' file

One more..

 awk '{print $1",""\t"$2}' test.txt