ChatGPT解决这个技术问题 Extra ChatGPT

sed - Commenting a line matching a specific string AND that is not already commented out

I have the following test file

AAA
BBB
CCC

Using the following sed I can comment out the BBB line.

# sed -e '/BBB/s/^/#/g' -i file

I'd like to only comment out the line if it does not already has a # at the begining.

# sed -e '/^#/! /BBB/s/^/#/g' file

sed: -e expression #1, char 7: unknown command: `/'

Any ideas how I can achieve this?


H
Hasturkun

Assuming you don't have any lines with multiple #s this would work:

sed -e '/BBB/ s/^#*/#/' -i file

Note: you don't need /g since you are doing at most one substitution per line.


This will always edit the matching line to begin with a single #. So if it began with three #s after this command it will "clean" it to just start with one #. To alleviate this problem if it is a problem, simply change the matching part accordingly (e.g. sed -e '/^BBB/...)
sed '/^#BBB/' you mean. But if BBB is somewhere in the middle it won't work. There is an option to write sed '/^#/! {/BBB/ s/^/#/}' which will work way better but my initial solution is way more simple as long as you know its limitation.
How to escape this if we are using above in java program?
Like this: sed -e '/BBB/ s|^(//)*|//|' -i file. Note that it only works for single-like comments, assumes that comment starts at the beginning of the line and will replace multiple pairs of // with a single //.
is it possible to put BBB in a input variable?
a
agostonbarna

Another solution with the & special character which references the whole matched portion of the pattern space. It's a bit simpler/cleaner than capturing and referencing a regexp group.

sed -i 's/^[^#]*BBB/#&/' file

wonderful thanks !
o
oguz ismail

I find this solution to work the best.

sed -i '/^[^#]/ s/\(^.*BBB.*$\)/#\ \1/' file

It doesn't matter how many "#" symbols there are, it will never add another one. If the pattern you're searching for does not include a "#" it will add it to the beginning of the line, and it will also add a trailing space.

If you don't want a trailing space

sed -i '/^[^#]/ s/\(^.*BBB.*$\)/#\1/' file

You may want to replace "!" with "^" but otherwise this is a better solution than the official answer
R
RayLuo

Assuming the BBB is at the beginning of a line, I ended up using an even simpler expression:

sed -e '/^BBB/s/^/#/' -i file

One more note for the future me. Do not overlook the -i. Because this won't work: sed -e "..." same_file > same_file.


B
Bilal

sed -i '/![^#]/ s/\(^.*BBB.*$\)/#\ \1/' file

This doesn't work for me with the keyword *.sudo, no comments at all...

Ony the syntax below works: sed -e '/sudo/ s/^#*/#/' file


I
Ibo

Actually, you don't need the exclamation sign (!) as the caret symbol already negates whatever is inside the square brackets and will ignore all hash symbol from your search. This example worked for me:

sed -i '/[^#]/ s/\(^.*BBB.*$\)/#\ \1/' file


Г
Геннадий Аполлонов

Comment all "BBB", if it's haven't comment yet.

sed -i '/BBB/s/^#\?/#/' file

A
Abolfazl Shahbazi

I'd usually supply sed with -i.bak to backup the file prior to making changes to the original copy:

sed -i.bak '/BBB/ s/^#*/#/' file

This way when done, I have both file and file.bak and I can decide to delete file.bak only after I'm confident.


P
P. D

If you want to comment out not only exact matches for 'BBB' but also lines that have 'BBB' somewhere in the middle, you can go with following solution:

sed -E '/^([^#].*)?BBB/  s/^/#/'

This won't change any strings that are already commented out.