Reputation: 131
Input:
echo "1021,fra,1022,eng,1023,qad" | sed or awk ...
Expected output:
1021-fra,1022-eng,1023-gad
Upvotes: 0
Views: 101
Reputation: 37298
echo "1021,fra,1022,eng,1023,qad" |sed 's/\([^,][^,]*\),\([^,][^,]*\)/\1-\2/g'
1021-fra,1022-eng,1023-qad
Upvotes: 3
Reputation: 41460
Here is an awk
version:
echo "1021,fra,1022,eng,1023,qad" | awk -F, '{for (i=1;i<NF;i++) printf "%s%s",$i,(i%2?"-":",");print $NF}'
1021-fra,1022-eng,1023-qad
Upvotes: 1
Reputation: 45293
by GNU sed
echo "1021,fra,1022,eng,1023,qad" |sed -r 's/([^,]+),([^,]+)/\1-\2/g'
Upvotes: 2
Reputation: 124704
Here's one way to do it, with a little cheat:
echo "1021,fra,1022,eng,1023,qad" | sed -e 's/,\([a-z]\)/-\1/g'
That is, replace every comma followed by a letter with a hyphen followed by that letter.
In case it helps, here's another version cheating a bit differently:
echo "1021,fra,1022,eng,1023,qad" | sed -e 's/\([0-9]\),/\1-/g'
That is, replace every digit followed by a comma with that digit and a hyphen.
Upvotes: 1