使用带有sed的特定字符串向文件中的每一行添加#

时间:2013-11-14 14:00:35

标签: bash search replace sed

我有一个看起来像这样的文件(只是它的一部分)

    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.

我想在包含字符串#center等的每行的开头添加cont。它们看起来都很相似,因此搜索center应该足够了。

如果另外我可以添加到脚本中,如果这些行已经包含一个很好的#,那么什么都不会发生,但这里不是非常重要。

输出应如下所示:

   #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.

我认为sed可以解决这个问题,但如果有人有更好的想法,我想在这里。

5 个答案:

答案 0 :(得分:3)

使用sed,您可以说:

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

这会导致:

    #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.

答案 1 :(得分:2)

使用sed,您可以使用center过滤/center/行,然后使用^过滤行#}的开头:

$ 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.

如果您需要#正好位于center文本的旁边,那么这就是:抓住空格,然后在#之前将它们打印回来。

$ 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.
...

如果您的-r中没有sed选项,则可以使用等效选项:

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

使用awk它会更快:您可以过滤包含center的行并将#添加到第一个字段:

$ 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.
...

答案 2 :(得分:1)

这很简单,使用sed:

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

发生了什么(来自tutorialspoint.comman sedsed 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.

同样的事情只是不生成备份文件(file.txt.bak):

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

答案 3 :(得分:1)

使用awk

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

答案 4 :(得分:0)

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

在这种情况下我们也可以尝试

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

假设文本行中没有数字或

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

假设数据行中没有字母