Add a # to every line in a file with certain string with sed

I have a file looking like this (just a part of it)

    center      cont      flux       eqw      core     gfwhm      fwhm                                                
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.                                                
  7372.827 0.3524984 -9.457E-4  0.002683 -0.011192   0.07938        0.                                                
  7384.392 0.3463771 -0.001513  0.004369 -0.024297   0.05851        0.                                                
  7384.655 0.3457934 -0.003066  0.008867 -0.037102   0.07763        0.                                                
  7387.274  0.347539 -0.014332   0.04124 -0.136604   0.09856        0.                                                
    center      cont      flux       eqw      core     gfwhm     lfwhm                                                
  7391.392 0.3548613 -0.044781    0.1262 -0.203154    0.2071        0.                                                
  7391.645 0.3539104 -0.008767   0.02477 -0.021864    0.3767        0.                                                
    center      cont      flux       eqw      core     gfwhm     lfwhm                                                
  7400.522 0.3491196 -4.204E-4  0.001204 -0.005909   0.06684        0.                                                
  7405.889  0.348969 -6.845E-4  0.001961 -0.009793   0.06566        0.

I want to add a # in the beginning of each line containing the strings center, cont, etc. They all look similar, so a search for center should be sufficient.

If in addition I could add to the script, that nothing will happen, if they line already contains a # that would be great, but not super important here.

The output should look like this:

   #center      cont      flux       eqw      core     gfwhm      fwhm                                                
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.                                                
  7372.827 0.3524984 -9.457E-4  0.002683 -0.011192   0.07938        0.                                                
  7384.392 0.3463771 -0.001513  0.004369 -0.024297   0.05851        0.                                                
  7384.655 0.3457934 -0.003066  0.008867 -0.037102   0.07763        0.                                                
  7387.274  0.347539 -0.014332   0.04124 -0.136604   0.09856        0.                                                
   #center      cont      flux       eqw      core     gfwhm     lfwhm                                                
  7391.392 0.3548613 -0.044781    0.1262 -0.203154    0.2071        0.                                                
  7391.645 0.3539104 -0.008767   0.02477 -0.021864    0.3767        0.                                                
   #center      cont      flux       eqw      core     gfwhm     lfwhm                                                
  7400.522 0.3491196 -4.204E-4  0.001204 -0.005909   0.06684        0.                                                
  7405.889  0.348969 -6.845E-4  0.001961 -0.009793   0.06566        0.

I think sed could solve this problem, but if anyone has a better idea I would like to here it.

Upvotes: 0

Views: 113

Answers (5)

cforbish
cforbish

Reputation: 8819

This is simple with sed:

 sed -i.bak '/center/s/^\([^#]\)/#\1/' file.txt

What is happening (from tutorialspoint.com, man sed and sed regex):

-i.bak     Edit files in place (makes backup if extension supplied)
/center/   Matches lines that contain the word center.
s/???/???/ Or s/regexp/replacement/, Attempt to match regexp against the pattern space.
/          Field separator to 's'.
^          Match first character on line.
\(         Start back reference.
[^#]       Do not match any charcter (^ = don't) in this list (only # listed).
\)         End back reference.
#          Literal '#'
\1         The first back reference.

Same thing only do not produce a backup file (file.txt.bak):

 sed -i '/center/s/^\([^#]\)/#\1/' file.txt

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed "/center/ !b;s/^\s*/&#/"

and in this case we also could try

sed "/[0-9]/ b;s/^\s*/&#/"

assumling there is no digit in text line or

sed "/[a-zA-Z]/ s/^\s*/&#/"

assuming there is not letter in data lines

Upvotes: 0

fedorqui
fedorqui

Reputation: 289995

With sed you can filter lines with center with /center/ and then on those replace the beginning of line (^) with #:

$ sed '/center/s/^/#/' file
#    center      cont      flux       eqw      core     gfwhm      fwhm
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.
  7372.827 0.3524984 -9.457E-4  0.002683 -0.011192   0.07938        0.
  7384.392 0.3463771 -0.001513  0.004369 -0.024297   0.05851        0.
  7384.655 0.3457934 -0.003066  0.008867 -0.037102   0.07763        0.
  7387.274  0.347539 -0.014332   0.04124 -0.136604   0.09856        0.
#    center      cont      flux       eqw      core     gfwhm     lfwhm
  7391.392 0.3548613 -0.044781    0.1262 -0.203154    0.2071        0.
  7391.645 0.3539104 -0.008767   0.02477 -0.021864    0.3767        0.
#    center      cont      flux       eqw      core     gfwhm     lfwhm
  7400.522 0.3491196 -4.204E-4  0.001204 -0.005909   0.06684        0.
  7405.889  0.348969 -6.845E-4  0.001961 -0.009793   0.06566        0.

And if you need the # to be exactly next to center text, then this makes it: catch the spaces and then print them back before the #.

$ sed -r '/center/s/^(\s*)/\1#/' file
    #center      cont      flux       eqw      core     gfwhm      fwhm
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.
...

If you do not have the -r option in your sed you can use the equivalent:

sed '/center/s/^\(\s*\)/\1#/' file

With awk it is even faster: you can filter lines containing center and add # to the first field:

$ awk '/center/ {$1="#"$1}1' file
#center cont flux eqw core gfwhm fwhm
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.
  7372.827 0.3524984 -9.457E-4  0.002683 -0.011192   0.07938        0.
...

Upvotes: 2

Jotne
Jotne

Reputation: 41460

Using awk

awk '{sub(/center/,"#&")}1' file

Upvotes: 1

devnull
devnull

Reputation: 123608

Using sed, you could say:

sed -r 's/^(\s+)(center)/\1#\2/g' filename

This would result into:

    #center      cont      flux       eqw      core     gfwhm      fwhm          
  7367.332 0.3494628 -0.002165  0.006196 -0.026459   0.07688        0.           
  7372.827 0.3524984 -9.457E-4  0.002683 -0.011192   0.07938        0.           
  7384.392 0.3463771 -0.001513  0.004369 -0.024297   0.05851        0.           
  7384.655 0.3457934 -0.003066  0.008867 -0.037102   0.07763        0.           
  7387.274  0.347539 -0.014332   0.04124 -0.136604   0.09856        0.           
    #center      cont      flux       eqw      core     gfwhm     lfwhm          
  7391.392 0.3548613 -0.044781    0.1262 -0.203154    0.2071        0.           
  7391.645 0.3539104 -0.008767   0.02477 -0.021864    0.3767        0.           
    #center      cont      flux       eqw      core     gfwhm     lfwhm          
  7400.522 0.3491196 -4.204E-4  0.001204 -0.005909   0.06684        0.           
  7405.889  0.348969 -6.845E-4  0.001961 -0.009793   0.06566        0.

Upvotes: 3

Related Questions