yandd
yandd

Reputation: 21

script to get a string after regex and replace it on each line with a match

So far have been able to extract the sting between the pattern match but havent been able to extend it to print as required.

My file content:

my number 12345 string1 string2
my number 789 string1 string2
my number 456789023 string3 string4

Next command

sed -e "s/.*number//;s/string.*//" filename

gives me output

 12345 
 789 
 456789023 

I need help to extend it or a better way to print output

my number 12345 num 12345 string1 string2
my number 789 num 789 string1 string2
my number 456789023 num 456789023 string3 string4 

Upvotes: 1

Views: 351

Answers (2)

potong
potong

Reputation: 58371

This might work for you:

sed 's/[0-9]\+/& num &/' file

Upvotes: 1

user unknown
user unknown

Reputation: 36229

From your example, I derive:

sed -r "s/number (.*) string/number \1 num \1 string/" FILE

Upvotes: 1

Related Questions