user2440648
user2440648

Reputation: 19

Modifying an input string using sed

Using sed, how can I change an input string like 9872 to 39 38 37 32 i.e. insert digit 3 before and a space after each digit of entered string 9872.

Input string:

9872   

Required output:

39 38 37 32

Upvotes: 0

Views: 91

Answers (3)

jaypal singh
jaypal singh

Reputation: 77185

$ echo "9872" | sed 's/[0-9]/3& /g' 
39 38 37 32 

Upvotes: 0

kirelagin
kirelagin

Reputation: 13626

And just for completeness, a more general way using regex references.

echo 9872 | sed -r 's/([[:digit:]])/3\1 /g'

Upvotes: 3

Claudio
Claudio

Reputation: 10947

echo 9872 | sed 's/./3&\ /g'

Upvotes: 4

Related Questions