user3072470
user3072470

Reputation: 131

How to group each 2 column together and chage the separator type between them?

Input:

echo "1021,fra,1022,eng,1023,qad" | sed or awk ...

Expected output:

1021-fra,1022-eng,1023-gad

Upvotes: 0

Views: 101

Answers (4)

shellter
shellter

Reputation: 37298

echo "1021,fra,1022,eng,1023,qad" |sed 's/\([^,][^,]*\),\([^,][^,]*\)/\1-\2/g'
1021-fra,1022-eng,1023-qad

Upvotes: 3

Jotne
Jotne

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

BMW
BMW

Reputation: 45293

by GNU sed

echo "1021,fra,1022,eng,1023,qad"  |sed -r 's/([^,]+),([^,]+)/\1-\2/g'

Upvotes: 2

janos
janos

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

Related Questions