Using Raku (formerly known as Perl_6)
Note, this answer has been updated in response to an excellent comment from @roaima.
The Raku code below tells you how many linewise matches to a Regex are found in a file. The given
/when
pattern is basically Raku's "switch" (or "case") statement. Once a match is found it is stored in the $/
(or $<>
) variable:
~$ raku -e 'my regex RE { ^^ v \s disable $$ };
given "/path/to/original.txt".IO.lines() {
when $_.elems > 0 and none / <RE> / { say "no match"; };
when $_.elems == 1 and / <RE> / { say "one line file, with match"; };
when $_.elems > 1 and / <RE> / { say "multi-line file, with match"; };
default {say "error"; }
};'
Thus (for example), in the third when
statement above ("multi-line file, with match"), the following code can be appended to the end of that when
block:
#`(
spurt("/path/to/original_bak.txt", $_.subst(:global, "$/ "), createonly => True);
unlink("/path/to/original.txt".IO) if "/path/to/original_bak.txt".IO ~~ :e & :f;;
copy("/path/to/original_bak.txt", "/path/to/original.txt", createonly => True)
)
Error handling is built-in for the three file-operations above: see spurt, class X::IO::Unlink, and class X::IO::Copy. You can add File Test Operator statements to test even more aspects.
The spurt
statement writes to a new _bak
file, unlink
unlinks the original, and copy
copies the _bak
backup file to the original name, leaving two files. Remove the multi-line comment designators (first and last lines), and change both instances of createonly
to False
once you're satisfied the code is working correctly (as judged by text return values).
You can do similar for "one line file, with match" case, or just change the conditional to combine two when
statements into one. If you don't like the one-liner format you can save as a script, make executable, run as a cron job, etc.
Below are previous answers (both Raku and Perl) which don't do error handling:
Raku: ~$ raku -ne '.put unless /^v\sdisable$/;' input.conf > tmp_input.conf
Perl: ~$ perl -ne 'print unless /^v disable$/;' input.conf > tmp_input.conf
These answers are pretty close to the sed
answer posted by @Panki, the advantage being here Perl/Raku will remove the blank line created. See the second link below for an explanation of Perl/Raku differences.
Perl can do -i
"in-place" replacement; Raku cannot. But you can adapt the third link below to use shell redirection to circumvent the Raku limitation (make backups first, in any case!).
NOTE: Raku isn't POSIX-compliant as the OP requests in an edit. I'm leaving this answer up for anyone seeking a non-POSIX approach.
https://docs.raku.org/language/5to6-nutshell#given-when
https://unix.stackexchange.com/a/749407/227738
https://unix.stackexchange.com/a/204378/227738
https://raku.org
if grep -q 'v disable' input.conf; then echo "found"; else echo "not found"
. Is that what you mean? Also a more sane way is to create a temp file then move the temp to the original file. So noinput.conf > input.conf
, ratherinput.conf > tmp.conf && mv -v tmp.conf input.conf
– Valentin Bajrami Jun 22 '23 at 09:35sed -i 's/^v disable$//g' input.conf
to modify the file in-place. – Panki Jun 22 '23 at 09:40sed -i.bak '/^v disable$/d'
might be what you want. Or for properly editing files you can useprintf '%s\n' '/^v disable/d' 'wq' | ed -s input.conf
– Valentin Bajrami Jun 22 '23 at 10:23