Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash Script to replace words

Tags:

linux

bash

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>
like image 441
Grimlockz Avatar asked Dec 23 '25 02:12

Grimlockz


2 Answers

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.

like image 192
potong Avatar answered Dec 24 '25 16:12

potong


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.

like image 21
ДМИТРИЙ МАЛИКОВ Avatar answered Dec 24 '25 15:12

ДМИТРИЙ МАЛИКОВ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!