Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

regex - Replacing from match to end-of-line

This should be incredibly easy but I can't get it to work. I just want to use sed to replace from one string to the end of a line. For example if I have the following data file:

   one  two  three  five
   four two  five five six
   six  one  two seven four

and I want to replace from the word "two" through the end of the line with the word "BLAH" ending up with the output:

   one BLAH
   four BLAH
   six one BLAH

wouldn't that just be:

   sed -e 's/two,$/BLAH/g'

I'm not the best at regex to maybe that's the problem

question from:https://stackoverflow.com/questions/5047165/replacing-from-match-to-end-of-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should do what you want:

sed 's/two.*/BLAH/'

$ echo "   one  two  three  five
>    four two  five five six
>    six  one  two seven four" | sed 's/two.*/BLAH/'
   one  BLAH
   four BLAH
   six  one  BLAH

The $ is unnecessary because the .* will finish at the end of the line anyways, and the g at the end is unnecessary because your first match will be the first two to the end of the line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...