[Solved] SED issue - making it "greedy" like in a regex

Issues related to applications and software problems and general support
Post Reply
lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

[Solved] SED issue - making it "greedy" like in a regex

Post by lightman47 » 2022/11/03 18:46:00

I have a script(s) which produce daily text outputs to a .txt file from a mariadb database. In the report sometimes there are NULL entries that are denoted with "/N". My SED command works very well if there is only ONE occurrence on a line, but if there are two NULLS I have to run the command a second time. Also, though it hasn't yet happened' there is a possibility of 3 NULLS in a line. Is there a 'switch' in SED to turn on greedy mode? The sed docs I've looked at are huge but I've not yet discovered the option for which I am searching. For now, the script runs the command twice.

Current command:

Code: Select all

sed -i s/'\\N'/'  '/ "$resultFile"
(I substitute the (same count) spaces for alignment purposes.)

Thank you - NO HURRY. I swear I used to know but must have forgotten.
Last edited by lightman47 on 2022/11/04 09:47:50, edited 1 time in total.

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: SED issue - making it "greedy" like in a regex

Post by TrevorH » 2022/11/03 18:57:02

s/string1/string2/g
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: SED issue - making it "greedy" like in a regex

Post by lightman47 » 2022/11/03 19:11:27

Yeh - that's a regex, but sed manpage: "g G Copy/append hold space to pattern space.", therefore I'd never tried it.

Will 'give it a shot' - thank you.

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: SED issue - making it "greedy" like in a regex

Post by lightman47 » 2022/11/03 19:37:09

OK, tried it - the report came out with lowercase g in the final field (comments):

Code: Select all

sed -i s/'\\N'/g'  '/ "$resultFile"
I'd rather not post personal info here, but I could PM it to you if you want to see the output.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: SED issue - making it "greedy" like in a regex

Post by jlehtone » 2022/11/03 19:47:38

s/string1/gstring2/
is not
s/string1/string2/g

Try:

Code: Select all

sed "s/\\N/  /g" "$resultFile"
info sed has entire section on the substitute command (s).

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: SED issue - making it "greedy" like in a regex

Post by lightman47 » 2022/11/03 20:01:10

:oops:

Thank you - I saw slashes and inserted a 'g' - how dumb!

Post Reply