Author Topic: sed help  (Read 1578 times)

sed help
« on: 20 April, 2020, 06:13:22 pm »
I'm trying to replace something in many files.

This command isn't cutting it.


Code: [Select]
sed -s 's/class="lr">1/class="lr">0/g' *.html
Where am I going wrong?

Code: [Select]
sed -n '/class="lr">1/p' <filename> finds the string in the file.
<i>Marmite slave</i>

Re: sed help
« Reply #1 on: 20 April, 2020, 06:28:14 pm »
sed doesn't replace things in situ

You probably want something like:-

*MAKE A BACKUP OF THE FILES*

Code: [Select]
for i in *.html ; do sed -e 's/class="lr">1/class="lr">0/g' $i > $i.new ; done

Then check that the .new files are correct and, if so, delete all the *.new files and do:-

Code: [Select]
for i in *.html ; do sed -e 's/class="lr">1/class="lr">0/g' $i > $i.new ; mv $i.new $i; done

Any problems revert to your backed up files.
"Yes please" said Squirrel "biscuits are our favourite things."

Re: sed help
« Reply #2 on: 20 April, 2020, 06:47:42 pm »
That seems to work. I owe you some beer tokens.

Numerous websites state that sed can be used to replace in-situ.


https://www.computerhope.com/unix/used.htm
https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/
<i>Marmite slave</i>

Re: sed help
« Reply #3 on: 20 April, 2020, 07:08:57 pm »
Newer versions of sed might support "-i" to do things in situ, but standard sed doesn't.

I spend my life working on systems I'm not in control of so it's far easier to learn how to do things with the basic tools that are common to all rather than new fangled ones that might be available on only some systems.
"Yes please" said Squirrel "biscuits are our favourite things."

Re: sed help
« Reply #4 on: 20 April, 2020, 08:19:02 pm »
Newer versions of sed might support "-i" to do things in situ, but standard sed doesn't.

I spend my life working on systems I'm not in control of so it's far easier to learn how to do things with the basic tools that are common to all rather than new fangled ones that might be available on only some systems.

That makes sense. I've used sed for this sort of task before quite successfully. Might have had a newish red hat environment loaded though.

Thank you again. Doing this is a dreadful hack, but it is only for a couple of builds.
<i>Marmite slave</i>

Re: sed help
« Reply #5 on: 21 April, 2020, 11:36:16 am »
Quote from: mrcharly-YHT link=topic=115427.msg2489529#msg2489529
Thank you again. Doing this is a dreadful hack, but it is only for a couple of builds.

Congratulations
You are now a DevOps Engineer with an Agile Mindset

Thor

  • Super-sonnicus idioticus
Re: sed help
« Reply #6 on: 21 April, 2020, 12:09:23 pm »
*MAKE A BACKUP OF THE FILES*

Good advice.  On more than one occasion I have done something like this, where the edited file was mv-ed back to the original in one pass, and I finished up with a directory full of zero-byte files  ???
It was a day like any other in Ireland, only it wasn't raining

Kim

  • Timelord
    • Fediverse
Re: sed help
« Reply #7 on: 21 April, 2020, 10:44:32 pm »
*MAKE A BACKUP OF THE FILES*

Good advice.  On more than one occasion I have done something like this, where the edited file was mv-ed back to the original in one pass, and I finished up with a directory full of zero-byte files  ???

Yeah, that's what happens when I try to use sed too.

Re: sed help
« Reply #8 on: 21 April, 2020, 11:11:37 pm »
perl can do in-place replace:
Code: [Select]
perl -pi -e 's/class="lr">1/class="lr">0/g' *.html

Re: sed help
« Reply #9 on: 22 April, 2020, 08:17:32 am »
perl can do in-place replace:
Code: [Select]
perl -pi -e 's/class="lr">1/class="lr">0/g' *.html

Perl . . .

Conversation with an intern:
GN, we have a project for you.
BN, it is in perl
GN, we have a book on Perl for you to study.
BN, the book is more than 5 years old.
GN, the book is fully up-to-date.
<i>Marmite slave</i>

vorsprung

  • Opposites Attract
    • Audaxing
Re: sed help
« Reply #10 on: 22 April, 2020, 09:18:10 am »
perl can do in-place replace:
Code: [Select]
perl -pi -e 's/class="lr">1/class="lr">0/g' *.html

and it can automatically make backup files, if needed