kiri
kiri

Reputation: 2642

Access match in sed search/replace?

I'd like to be able to replace 1 with 1. in a block of text I have with varying numbers.

So, how can I find out the matched digit to use in the replace?

Here's an example :

echo "1   MORE TEXT HERE" | sed 's/[0-9]/[0-9]\./g' 

I'd like the [0-9] in the 2nd part of sed to give me the match.

Upvotes: 3

Views: 166

Answers (2)

Jotne
Jotne

Reputation: 41456

A solution using gnu awk

echo "1   MORE TEXT HERE" | awk '{print gensub(/[[:digit:]]*/,"&.","")}'
1.   MORE TEXT HERE

Upvotes: 0

Andy Ray
Andy Ray

Reputation: 32066

echo "1 MORE TEXT HERE" | sed 's/[0-9]/&\./g'

& is the full match

Upvotes: 5

Related Questions