Hi, I have the following code:
!/bin/bash
while read line;
do
echo "$line";
if[???????]
fi
done < /home/javi.log
javi.log file:
#
javi_2223
javi55_555
imp33
imp44
javi32423
javi5646
I want the script to comment the lines imp33 and imp44 I know what would be done with if but I don't know what to use to comment them...
It would be as follows:
#
javi_2223
javi55_555
#imp33
#imp44
javi32423
javi5646
if the file were like this: how would it be putting the :space it does not take it for me
javi_2223
javi55_555
#imp33
#imp44
javi32423
javi5646
Wearing
bash
To get the best result
bash
and to make it easier to add new terms, I recommend you usecase
instead ofif
nested ones as follows:Wearing
sed
The most efficient way to do what you want is to use the tool
sed
using the following substitution pattern:Of all the functionality it offers,
sed
I have used the "substitution" function (s
from substitute ) which has the following format:s/patrón/sustitución/opciones
. In my case I haven't used any option, but you could usei
, for example, to make the search case insensitive.In the online version you will see a description of how the pattern and capture groups work:
^
.()
:imp33
.imp44
.$
.Whatever is on the line, if the capture pattern is met, will be replaced by
#\1
what it means:#
.\1
.Unlike the version in
bash
, blank characters before and after the text are not removed, so if you want the behavior to be the same you should use the following pattern:In the online version you will see a description of how the pattern and capture groups work:
^
.[ \t\r\n\v\f]
).()
:imp33
.imp44
.[ \t\r\n\v\f]
).$
.As the blank characters have not been included in the capturing group they will not be in the substitution, which will remain the hash character
#
followed by the capturing group.