sed Find and Replace Text Between Two Strings or Words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed Find and Replace Text Between Two Strings or Words
# 1  
Old 12-07-2015
sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder.

For instance, the 1st indicator is a list of words
Code:
"no|noone|havent"

and the 2nd indicator is a list of punctuation
Code:
 ".|,|!".

From a sentence such as

Code:
"noone understands me."

I want to recognize "understands me" and add an affix onto the end of each word, desired result being
Code:
"noone understands_AFFIX me_AFFIX."

.

I know that there is the sed:
Code:
sed -n '/WORD1/,/WORD2/p' /path/to/file

which recognizes the content between two indicators, but I am not able to adapt it properly to accurately and efficiently solve my specific issue. Does anyone have a suggestion?

Last edited by owwow14; 12-07-2015 at 01:00 PM..
# 2  
Old 12-07-2015
Where has your second indicator gone in the result?
I don't think you can do that with sed, but e.g. awk would lend itself to solve your ptoblem.
# 3  
Old 12-07-2015
Sorry @RudiC when I was writing the question, I forgot to include it when copying and pasting.

I was not aware that this could be done with awk . How would you suggest attacking this problem using that?
# 4  
Old 12-07-2015
Given that
- indicator 1 & 2 arein line 1 & 2 of file1
- the indicator2 (= period in this sample) is a word of its own in file2
you can try
Code:
awk '
NR==1   {for (n=split($0, T, "|"); n>0; n--) S1[T[n]]
         next
        }
NR==2   {for (n=split($0, T, "|"); n>0; n--) S2[T[n]]
         next
        }
        {for (i=1; i<=NF; i++)  {if ($i in S1)  {L = 1
                                                 continue
                                                }
                                 if ($i in S2)   L = 0
                                 if (L)  $i = $i "_AFFIX"
                                }
        }
1
' file1 file2
noone understands_AFFIX me_AFFIX .

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

2. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

3. Shell Programming and Scripting

[Solved] sed to replace words

Hello All, I have file named filelist.txt a.bteq.ctl b.bteq.ctl c.bteq.ctl I want to replace the word bteq to tpt in this file. I used this sed command cat filelist.txt | sed 's/bteq/tpt/g' > filelist.txt But this command deletes all records from the filelist.txt Can... (2 Replies)
Discussion started by: nnani
2 Replies

4. Shell Programming and Scripting

sed help, Find a pattern, replace it with same text minus leading 0

HI Folks, I'm looking for a solution for this issue. I want to find the Pattern 0/ and replace it with /. I'm just removing the leading zero. I can find the Pattern but it always puts literal value as a replacement. What am I missing?? sed -e s/0\//\//g File1 > File2 edit by... (3 Replies)
Discussion started by: SirHenry1
3 Replies

5. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

6. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

7. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

8. Shell Programming and Scripting

sed to find replace mutliline text

Hi, My input file form 1 fill 2 fill 3 form 4 fill 5 form 6 fill 7 form 8 Now i need to substiute according to the fill. form followed by single fill need to be replced with category 1 form with above and below fill need to be repalced with category 2 (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

9. UNIX for Dummies Questions & Answers

replace words in sed using regular expression

hi, I need to replace all these lines from my text file 123end 234end 324end 234end 989end 258end 924end At the moment I know how to replace "end". But I want to replace the numbers before end as well. How can I do this ? sed s/end/newWord/ myfile.txt newFile.txt thanks (3 Replies)
Discussion started by: aneuryzma
3 Replies

10. UNIX for Dummies Questions & Answers

sed replace words in file and keep some

lets see if i can explain this in a good way. im trying to replace some words in a file but i need to know what the words are that is beeing replaced. not sure if sed can do this. file.name.something.1DATA01.something.whatever sed "s/./.DATA?????/g" need to know what the first . is... (2 Replies)
Discussion started by: cas
2 Replies
Login or Register to Ask a Question