bash scripting question

Issues and / or general discussion relating to Puppy

Moderator: Forum moderators

Post Reply
User avatar
spiritwild
Posts: 85
Joined: Wed Jul 22, 2020 1:34 am
Been thanked: 27 times

bash scripting question

Post by spiritwild »

I don't code much but I'm looking for a way to replace everything between <p> and </p> where the word hello is with something from a text file.
since the text between the paragraph element will change every time, I got stumped
I was looking at the "sed" command initially.

This is the line:

"htmlContent":"<html><head></head><body><p>Hello,</p></p></body></html>"

Thanks. :thumbup:

User avatar
rockedge
Site Admin
Posts: 6539
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2746 times
Been thanked: 2620 times
Contact:

Re: bash scripting question

Post by rockedge »

@spiritwild, Can you test this please?

Code: Select all

sed -i 's/^<p>=/&something_from_a_text_file /' name_of_the_file_to_modify.html

Might need some refinement..... Like adding in a <p> in front of something_from_a_text_file

so far it's not doing it...needs some adjustment

xx_T3n0ch_X
Posts: 36
Joined: Thu Jul 22, 2021 1:31 am
Has thanked: 3 times
Been thanked: 10 times

Re: bash scripting question

Post by xx_T3n0ch_X »

This should work in many situations, we replace Hello with OtherWord using awk's general substitution function.

Code: Select all

awk  '/<p>/{f=1} f==1{gsub(/Hello/,"OtherWord"); print $0} /<\/p>/{f=0}' filename.html > edited_file.html

f=1 and f=0 are 'on and off switches' we create to differentiate the lines where we want to apply substitutions, there could be a word Hello incrusted in a <H1></H1> tag and perhaps we do not want a substitution there.

In its basic form awk reads line by line a stream and tries to find a pattern to then execute some action or actions, you can have more than one pattern/action, so in the code snippet above we have three pattern/actions:

1st. /<p>/{f=1}, scan for the string "<p>", if you find it, then create and assign f=1

2nd. f==1{gsub(/Hello/,"OtherWord"); print $0}, if the value of f is 1 then substitute Hello for OtherWord, and then print the line

3rd. /<\/p>/{f=0}, scan for the string "</p>", if you find set variable f to 0, f=0

Further reading: https://www.gnu.org/software/gawk/manual/gawk.html

results from terminal,

Code: Select all

root# echo '"htmlContent":"<html><head></head><body><p>Hello,</p><p>Hello</p></body></html>"' | awk  '/<p>/{f=1} f==1{gsub(/Hello/,"OtherWord"); print $0} /<\/p>/{f=0}'

"htmlContent":"<html><head></head><body><p>OtherWord,</p><p>OtherWord</p></body></html>"

Last edited by xx_T3n0ch_X on Mon Jul 04, 2022 4:03 pm, edited 2 times in total.
HerrBert
Posts: 357
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 126 times

Re: bash scripting question

Post by HerrBert »

May not be helpful, but your code isn't valid html.
There is one opening <p> tag but two closing </p> tags.

User avatar
rockedge
Site Admin
Posts: 6539
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2746 times
Been thanked: 2620 times
Contact:

Re: bash scripting question

Post by rockedge »

This will work in a terminal:

Code: Select all

echo 'htmlContent":"<html><head></head><body><p>Hello,</p></p></body></html>' | sed 's/Hello/Goodbye/'

or make a test.html file with this in it:

Code: Select all

htmlContent":"<html><head></head><body><p>Hello,</p></p></body></html>

then a script test.sh that is next to test.html

Code: Select all

#!/bin/sh
sed -i 's/Hello/Goodbye/' test.html
Last edited by rockedge on Sun Jul 03, 2022 9:26 pm, edited 2 times in total.
Reason: removed extra spaces in SED code
HerrBert
Posts: 357
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 126 times

Re: bash scripting question

Post by HerrBert »

Remove tags:

Code: Select all

# echo "htmlContent":"<html><head></head><body><p>Hello,</p></p></body></html>" | sed 's%<[^>]*>%%g'
htmlContent:Hello,

Remove leading text and tags:

Code: Select all

# echo "htmlContent":"<html><head></head><body><p>Hello,</p></p></body></html>" | sed 's%.*:%%;s%<[^>]*>%%g'
Hello,

Will probably not work with other than the provided code...
@spiritwild
You have to be more precise on the searchpattern...

Quick link for basic sed commands: http://sed.sourceforge.net/sed1line.txt

User avatar
rockedge
Site Admin
Posts: 6539
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2746 times
Been thanked: 2620 times
Contact:

Re: bash scripting question

Post by rockedge »

This type of question is good start point for experimentation to learn how to do this.

On a php-html file I ran a very simple search for just Hello and replace it with Goodbye

Just to use some fill I am using this php-html that takes 3 camera streams from Zoneminder and displays them on a web page. I added in a fake PHP section to test out finding the pattern "Hello" and swapping it out.

Test-SED.php

Code: Select all

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">	
<style type='text/css'>
body {background-color: black;}

#containercams {
	text-align: center;
}
#containercams > div {

	display: inline-block;
	vertical-align: top;

	/* IE fix. */
	*display: inline;
	zoom: 1;
}
#containercams:after {
	content: "";
	width: 100%;
	display: inline-block;
}

</style>
</head>

<?php
$content_string = 'htmlContent":"<html><head></head><body><p>Hello,</p></body></html>';
?>

<body>
<div id=containercams>
<div><img class="alignnone" src="http://localhost/cgi-bin/nph-zms?mode=jpeg&maxfps=&scale=75&state=1&monitor=1&auth=a732c9fb04ac5339208cdeedc9bbf94b&rand=1656735805&connkey=704099" alt="stream down" width="300" height="220"></div>
<div><img class="alignnone" src="http://localhost/cgi-bin/nph-zms?mode=jpeg&maxfps=&scale=75&state=1&monitor=2&auth=a732c9fb04ac5339208cdeedc9bbf94b&rand=1656735862&connkey=324859" alt="stream down" width="300" height="220"></div>
<div><img class="alignnone" src="http://localhost/cgi-bin/nph-zms?mode=jpeg&maxfps=&scale=100&state=1&monitor=3&auth=a732c9fb04ac5339208cdeedc9bbf94b&rand=1656735913&connkey=61446" alt="stream down...." width="300" height="220"></div>
</div>
</body>

The script:
test.sh

Code: Select all

#!/bin/sh
sed -i 's|Hello|Sometimes you want to swap out a pattern|' Test-SED.php
Burunduk
Posts: 251
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 127 times

Re: bash scripting question

Post by Burunduk »

This should work iff:

  • each <p>...</p> pair is always on the same line (it's possible to work around this if you need)

  • <p>...</p> doesn't contain any html tag inside (any "<" actually)

In the replacement text from the something.txt, newline chars will be replaced with spaces.

Code: Select all

text=$(sed ':a;$!{N;ba};{s|[\/&]|\\&|g;s/\n/ /g}' something.txt)
sed  -i "s/<p>[^<]*hello[^<]*<\/p>/<p>${text}<\/p>/ig" content.html

Edit: only "/" was escaped in the replacement but I forgot about "\" and "&" Should be better now.

User avatar
spiritwild
Posts: 85
Joined: Wed Jul 22, 2020 1:34 am
Been thanked: 27 times

Re: bash scripting question

Post by spiritwild »

Thanks for the input, all very helpful. sed is quite a mind trip.
I was using google for smtp but they finally ditched the setting for trusted apps. Found a new smtp server and all is well now. I use it to text myself or email info from an old "pupmemo" code I modified

I was playing around with curl commands. Works but I never got it where I needed for my setup.

Thanks!!

Post Reply

Return to “Users”