Reputation: 2581
Looking to run a script on the server to look at a path of a file and replace a word whereas it matches in the div.
So need to replace _toself to viewers where author equals a certain email [email protected]
URL=/var/www/sever/temp/fhyw1 FILE=user.txt
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>_toself</div>
<div class='num'>4</div>
</div>
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>_viewers</div>
<div class='num'>4</div>
</div>
Upvotes: 3
Views: 286
Reputation: 58371
This sed solution might work for you:
sed -e '/^<div class=.entry.>/,\_^</div>_{//!{H;d};\_^</div>_!{h;d};x;/author.>[email protected]/s/_toself/SUBSTITUTE TEXT/;p;x}' text_file
N.B. You will need to replace SUBSTITUE TEXT
with the viewers
,_viewers
or whatever
The sed
command allows all lines other than those between <div class=.entry.>
and </dev>
(.
allows for single '
or double quotes "
) to pass through unchanged. If the line begins with <div class=.entry.>
it is copied to a register call the hold space (HS) and then the pattern space (PS) is deleted. All other lines are appended to the HS and then deleted accepting the line </div>
. When this line appears the HS is swapped with the PS and if this multiline contains author.>[email protected]
then SUBSTITUTE TEXT
is substituted for _toself
. The multiline is printed out regardless, then the PS replaces the HS and it in turn to printed out.
Upvotes: 1
Reputation: 21972
We have some text
$> cat ./text
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>_toself</div>
<div class='num'>4</div>
</div>
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>_viewers</div>
<div class='num'>4</div>
</div>
And we need to replace _toself
'to' value with viewers
, but only in divs, where 'author' equals a [email protected]
I think sed can helps you, but you should have some experience with it to formulate all condition with sed syntax.
So, we can read file in while loop, cut it into a div-blocks and change one value by another only if blocks 'authors' value is equal some email.
#!/bin/bash
mail="[email protected]"
to_value_old=_toself
to_value_new=viewers
while IFS= read -r line; do
if [[ -z "$( echo "$line" | grep -o -P "^<\/div>$" )" ]]; then
entry_block="${entry_block}${line}\n"
else
entry_block="${entry_block}</div>\n"
entry_block="$( echo -e "${entry_block}" )"
if [[ -n "$( echo "${entry_block}" | grep -P "\<div class=\'author\'\>${mail}\<\/div\>" )" ]]; then
entry_block="$( echo "${entry_block}" | sed -r -e "s/<div\ class='to'>${to_value_old}<\/div>/<div\ class='to'>${to_value_new}<\/div>/" )"
fi
echo "${entry_block}"
entry_block=""
fi
done < ./text
And we get
$> ./div.sh
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>viewers</div>
<div class='num'>4</div>
</div>
<div class='entry'>
<div class='pageurl'>temp/fhyw1</div>
<div class='context'>text</div>
<div class='subject'>testing</div>
<div class='notetext'></div>
<div class='signed'>USER</div>
<div class='author'>[email protected]</div>
<div class='color'>0</div>
<div class='visibility'>shared</div>
<div class='to'>_viewers</div>
<div class='num'>4</div>
</div>
Done.
Upvotes: 1
Reputation: 37427
If you only want to replace all occurrences of _toself
by something else, then sed
will do the job perfectly.
sed 's/_toself/replacement_string/'
If you want to do this only within the div with a specified author
then it's a bit more tricky.
Upvotes: 0