巴什 - 查找并在同一个文件中的新行替换 [英] Bash - Find and replace with new lines in the same file

查看:88
本文介绍了巴什 - 查找并在同一个文件中的新行替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到包含特定的变量名的文件中的行。该变量可以被注释掉或没有。如果没有,那么我的评论需要评论它。加入两行之后。第一行开始为什么添加了新的生产线,然后用该变量的新生产线的附加价值评论。我使用Ubuntu 01年4月14日x64和它的版本(GNU SED)4.2.2和GNU awk的4.0.1。此外,我想用先前在脚本中定义的变量。因此,命令必须接受引用这些变量。另外不要将结果输出到控制台。一些实例是:

案例1

  #ssh_param /路径/要/文​​件

结果

  #ssh_param /路径/要/文​​件#新路径定义
ssh_param的/ etc / SSH /文件

案例2

  ssh_param /路径/要/文​​件

结果

  #ssh_param /路径/要/文​​件#新路径定义
ssh_param的/ etc / SSH /文件

案例3

  #ssh_param

结果

  #ssh_param#新路径定义
ssh_param的/ etc / SSH /文件

案例4

  #ssh_param dasfda
ssh_param /路径/要/文​​件

结果

  #ssh_param dasfda
#ssh_param /路径/要/文​​件#新路径定义
ssh_param的/ etc / SSH /文件


解决方案

您想用先前在脚本中定义的变量。下面是变量:

  file =文件路径/要/输入/文件
VAR =ssh_param
味精=#新路径定义
新=/等/ SSH /文件

下面是其中使用这些变量的命令:

  LINENO = $(grep的-n$ VAR$文件|尾-n1 | sed中的/:.*//')
如果[$ LINENO]
然后
    SED -i -r$ LINENO S |#?(。*)|#\\ 1 \\ n $的味精\\ N $变量$新| $文件
其他
    SED -i -r$ S | $ | \\ n $的味精\\ N $变量$新| $文件
科幻

注:


  • 这似乎从你的例子,变量名在脚本中出现多次,但只有最后一个是注释。我们用在这里来简化code。在脚本变量的最后出现的行号是从实测值:

      LINENO = $(grep的-n$ VAR$文件|尾-n1 | sed中的/:.*//')

    的grep -n 部分找到哪些变量名出现行号的列表。 尾-n1 只返回最后一个这样的线路。在 SED 命令删除所有,但在输出的行号。


  • 该替代由该命令来完成:

      SED -i -r$ LINENO S |#?(。*)|#\\ 1 \\ n $的味精\\ N $变量$新| $文件'

    本使用的sed的在一个特定的行号运营的能力。这条线由三条线取代:首先是现有生产线的副本,但评论说,第二个是信息,第三个是变量的新定义


  • 如果变量不是当前的文件,那么 LINENO 是空的。在这种情况下,我们希望将新行添加到文件的末尾。这是通过一个类似但更简单 SED 命令来完成:

      SED -i -r$ S | $ | \\ n $的味精\\ N $变量$新| $文件


第一个 $ 告诉 SED 仅在最后一行运行。第二个 $ 出现在它标志着一行的末尾替换命令。您的消息和新的变量定义被追加。

示例

以此为输入:

  #ssh_param dasfda
ssh_param /路径/要/文​​件

以上code替换文件用:

  #ssh_param dasfda
#ssh_param /路径/要/文​​件
#新路径定义
ssh_param的/ etc / SSH /文件

(如果你想空行空间的东西出来,只需添加一些 \\ n SED 输出前pression。)

I need to find a line in a file that contains a specific variable name. This variable may be commented or not. If not commented then I need to comment it. Add two lines afterwards. The first line begins with a comment on why the addition of the new line then the new line of the variable with attach value. I'm using ubuntu 14.04.01 x64 and it has version (GNU sed) 4.2.2 and GNU Awk 4.0.1 . Also I would like to use variable defined earlier in the script. So command needs to be accept reference to those variables. Also do not output the result to console. Some examples are:

Case 1

#ssh_param /path/to/file

Results

#ssh_param /path/to/file

#New path defined
ssh_param /etc/ssh/file

Case 2

ssh_param /path/to/file

Result

#ssh_param /path/to/file

#New path defined
ssh_param /etc/ssh/file

Case 3

#ssh_param 

Results

#ssh_param 

#New path defined
ssh_param /etc/ssh/file

Case 4

#ssh_param dasfda
ssh_param /path/to/file

Results

#ssh_param dasfda
#ssh_param /path/to/file 

#New path defined
ssh_param /etc/ssh/file

解决方案

You wanted to use "variables defined earlier in the script." Here are the variables:

file="path/to/input/file"
var="ssh_param"
msg="#New path defined"
new="/etc/ssh/file"

Here are the commands which use those variables:

lineno=$(grep -n "$var" "$file" | tail -n1 | sed 's/:.*//')
if [ "$lineno" ]
then
    sed -i -r "$lineno s|#?(.*)|#\1\n$msg\n$var $new|" "$file"
else
    sed -i -r "$ s|$|\n$msg\n$var $new|" "$file"
fi

Notes:

  • It appears from your examples that the variable name may occur multiple times in the script but only the last one is uncommented. We use that here to simplify the code. The line number of the last appearance of the variable in the script is found from:

    lineno=$(grep -n "$var" "$file" | tail -n1 | sed 's/:.*//')
    

    The grep -n part finds a list of line numbers on which the variable name occurs. tail -n1 returns only the last such line. The sed command removes all but the line number from the output.

  • The substitution is done by this command:

    sed -i -r "$lineno s|#?(.*)|#\1\n$msg\n$var $new|" "$file"`
    

    This uses sed's ability to operate on a particular line number. That line is substituted by three lines: the first is a copy of the existing line but commented, the second is the message, and the third is the new definition of the variable.

  • If the variable is not currently in the file, then lineno is empty. In this case, we want to add the new lines to the end of the file. This is done via a similar but simpler sed command:

    sed -i -r "$ s|$|\n$msg\n$var $new|" "$file"
    

The first $ tells sed to operate only on the last line. The second$ appears in the substitute command where it signifies the end of that line. Your message and new variable definition are appended.

Example

With this as the input:

#ssh_param dasfda
ssh_param /path/to/file

The above code replaces the file with:

#ssh_param dasfda
#ssh_param /path/to/file
#New path defined
ssh_param /etc/ssh/file

(If you want blank lines to space things out, just add some \n to the sed output expression.)

这篇关于巴什 - 查找并在同一个文件中的新行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆