Replace string between words multiple times in a file

Atom

I am trying to replace string between two strings in a file with the command below. There could be any number of such patterns in the file. This is just an example.

sed 's/word1.*word2/word1/' 1.txt 

There are two instances where 'word1' followed by 'word2' occurs in the sample source file I'm testing. Content of the 1.txt file

word1---sjdkkdkjdk---word2 I want this text----word1---jhfnkfnsjkdnf----word2 I need this also

Result is as below.

word1 I need this also

Expected Output :

 word1 I want this text----word1 I need this also

Can anybody help me with this please?

I looked at other stack-overflow questionnaire but they discuss about replacing only one instance of the pattern.

Shawn

Regular expressions are greedy - they match the longest possible string, so everything from the first 'word1' to the last 'word2'. Not sure if any version of sed supports non-greedy regexps... you could just use perl, though, which does:

perl -pe 's/word1.*?word2/word1/g' 1.txt

should do the trick. That ? changes the meaning of the prior * from 'match as many times as possible as long as the rest of the pattern matches' to 'match as few times as possible as long as the rest of the pattern matches'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find/replace string between two words in text file

Replace multiple words on string using Map of replacements

Python Regex - replace a string not located between two specific words

Catch everything between two same words multiple times

Extract string between multiple words, using gsub

Kotlin replace multiple words in string

find and replace multiple words in a file python

Replace file text between two words across multiple lines

Replacing a string between strings multiple times

replace multiple words in string with specific words from list

Replace multiple different words in string

Extract a string of words between multiple specific words in R

How to get a string between a variable string and a constant string multiple times

string replace multiple words in one sentence

perl program to replace a string in a file multiple times

How to replace string with random line from text file multiple times

Find and replace multiple times in txt file

Replace words in string by multiple scans

Replace string between two words with the content of another text file

Replace multiple words in string with dictionary (python)

Replace words in html element multiple times

regex replace space between words in string

linux find and replace multiple times in file

Python: replace all characters in between two symbols that occur multiple times in a string

Extract a string between two words, with multiple patterns

Grep on a file to find a string between two words

Generate multiple words from string reading an file

Substring between specific characters in a string multiple times

Replace words in string between two specific chars