Comment a line in a file.log separated by blank spaces? [duplicate]

0

file.log

if $programname contains 'file_apps_' then  @233.33.3.3

$ModLoad fdsf

#File APPS  - j_d
action(type="dfsfdsf")
$InputFileName /logs/.log
$InputFileTag ja_dd_d
$InputFileStateFile jas_dd_d
$InputFileSeverity info
$InputFileFacility local7
$InputRunFileMonitor


#File APPS  - j_d
#action(type="dfsfdsf")
#$InputFileName /logs/.log
#$InputFileTag ja_dd_d
#$InputFileStateFile jas_dd_d
#$InputFileSeverity info
#$InputFileFacility local7
#$InputRunFileMonitor

I want the lines to be commented on

$InputFileName /logs/.log
$InputFileTag ja_dd_d

could be done with a thirst but I tried and nothing ...

    
asked by ortiga 20.12.2018 в 21:07
source

1 answer

2

A simple way to use sed to comment on the lines that match a certain pattern is to "select" those lines with a regular expression, and "apply" the substitution of the "start of line" with the character # . That is:

sed '/expresion-regular/s/^/#/g' < entrada > salida

The "quiz" here is the regular expression that you should use, and what version of sed uses as the details of the syntax vary between versions.

The regular expression can be as simple as a word, but in this case it seems that there are two that you are looking for. A regular expression like the following will select the lines that begin with "$InputFileName" or "$InputFileTag" :

/^(\$InputFileName|\$InputFileTag)/

In fact, since both chains start the same, you can also put it like this:

/^\$InputFile(Name|Tag)/

Notice that the $ must have a \ in front to "escape" its special meaning in a regular expression.

Now, to use that regular expression in sed , you have to add some extra characters depending on what version of sed uses:

  • GNU sed (It's the version that comes on Linux)

    In this version you must also escape the parentheses and the | , so you should use:

    sed '/^\$InputFile\(Name\|Tag\)/s/^/#/g' < entrada > salida
    
  • BSD sed (it's the version that comes on OSX)

    In this version there is no need to escape parentheses, and the | does not understand it by default, but it will admit it if you add the option -E . Therefore:

    sed -E  '/^\$InputFile(Name|Tag)/s/^/#/g' < entrada > salida
    

Once you have checked (looking at the file salida ) that has worked correctly, you can change the syntax for this one, to directly modify the input file:

sed -i '..lo_antes_dicho...'  entrada
    
answered by 20.12.2018 / 21:40
source