Bash 使用sed将#添加到具有特定字符串的文件中的每一行

Bash 使用sed将#添加到具有特定字符串的文件中的每一行,bash,search,replace,sed,Bash,Search,Replace,Sed,我有一个这样的文件(只是其中的一部分) 我想在包含字符串center,cont等的每行开头添加一个#。它们看起来都很相似,因此搜索center就足够了 此外,如果我可以添加到脚本中,那么什么也不会发生,如果它们的行已经包含一个#,这将是非常好的,但在这里并不是非常重要 输出应如下所示: #center cont flux eqw core gfwhm fwhm

我有一个这样的文件(只是其中的一部分)

我想在包含字符串
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
可以解决这个问题,但如果有人有更好的想法,我想把它放在这里。

使用
sed
可以使用
/center/
过滤带有
/center
的行,然后在这些行上用
替换行的开头(
^
):

如果您需要将
#
正好放在
中心
文本的旁边,那么这样做就是:捕捉空格,然后将它们打印回
#
之前

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

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

使用
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.

这对于sed很简单:

 sed -i.bak '/center/s/^\([^#]\)/#\1/' file.txt
发生了什么(来自和):

同样的事情只会产生一个备份文件(file.txt.bak):

使用
awk

awk '{sub(/center/,"#&")}1' file
在这种情况下,我们也可以尝试

sed "/[0-9]/ b;s/^\s*/&#/"
假设文本行或文本中没有数字

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

假设数据行中没有字母

这里不关心速度,但感谢非常好的示例和解释。它工作得很好,但就目前而言,cforbish的方法是最好的。没问题:)我看不出两者之间有什么区别,除了他正在使用
-I
进行就地编辑。在这种情况下,不需要最后一个“g”(仅1个替换)Errr你是对的,@NeronLeVelu相应地更新。谢谢
-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.
 sed -i '/center/s/^\([^#]\)/#\1/' file.txt
awk '{sub(/center/,"#&")}1' file
sed "/center/ !b;s/^\s*/&#/"
sed "/[0-9]/ b;s/^\s*/&#/"
sed "/[a-zA-Z]/ s/^\s*/&#/"