Remove exact line from file if present, leave the rest of lines; error-handling - how to go about it
1
vote
2
answers
346
views
I am not exactly sure how text processing should be done in a good way, so let me ask.
I have this file:
~/.config/mpv/input.conf
containing possibly other options as well as v disable
.
If I want to remove that line from the file, I suppose I could do:
-sh
grep -v -q -F 'v disable' input.conf; echo $?
1
Where - if the file contains nothing else like now, it would return 1 (man page says 1
- "No lines were selected."), which troubles me as I cannot run a simple if-statement
.
So I would have to store $?
after the command and then check if it is greater than 1 for error.
Also, it is unclear to me, if replacing the file in-place is a good idea:
-sh
grep ... input.conf > input.conf
---
If there is some other way, let me know. Also if portions of my suggestions are correct, please let me know.
Thanks, and cheers.
---
EDIT1:
Desired behavior is to toggle between the state where there is this line (done with simple >>
addition after a check if it is there, and is not (remove it, thus this question).
EDIT2:
The accepted solution must be POSIX-compliant. No _bashisms_ or non-POSIX tools. You can, however, include non-POSIX solutions which might be useful for other people. In any case, thanks.
Asked by Vlastimil Burián
(30515 rep)
Jun 22, 2023, 09:24 AM
Last activity: Jan 11, 2025, 09:48 AM
Last activity: Jan 11, 2025, 09:48 AM