【问题标题】:How to add a new string at the end of line after searching a string in that line in a file in Unix在Unix中的文件中搜索该行中的字符串后如何在行尾添加一个新字符串
【发布时间】:2014-07-15 14:56:07
【问题描述】:

我有一个文件,它是我们服务器中所有 autosys 作业的 jil 代码的摘录。

jil 代码示例:

permission:gx,wx  
date_conditions:yes  
days_of_week:all  
start_times:"05:00"  
condition: notrunning(appDev#box#ProductLoad)

我们想为服务器中的所有作业添加一个新的依赖条件。
因此,我们需要在行尾添加一个新字符串(具有依赖条件的行),如下所示,然后将作业重新加载到服务器:

condition: notrunning(appDev#box#ProductLoad) & s(new_job_dependency)

我们在服务器中有 10000 多个作业(以及文件中的 100K 行数据),无法逐行读取并进行更改。
我们必须在行首搜索字符串“condition:”,然后在同一行末尾添加新字符串“new_job_dependency”。

有人可以建议一种方法来实现这一点吗?

【问题讨论】:

  • S.O.不是免费的编码服务,您应该付出最小的努力来解决您的问题。 10000 多个工作岗位?也许你最好聘请一位顾问。祝你好运。

标签: shell unix autosys


【解决方案1】:

用一些 perl 魔法替换所有这些怎么样

perl -i -pe  's#^condition: (.*)$#condition: $1 & s(new_job_dependency)#g' filename

【讨论】:

    【解决方案2】:

    sed可以帮你:

    $ sed '/^condition:/s/$/ \& s(new_job_dependency)/' file
    permission:gx,wx  
    date_conditions:yes  
    days_of_week:all  
    start_times:"05:00"  
    condition: notrunning(appDev#box#ProductLoad) & s(new_job_dependency)
    

    如果您希望动态更新文件并创建.bak 备份文件,您可以添加-i.bak

    sed -i.bak '/^condition:/s/$/ \& s(new_job_dependency)/' file
    

    要将其应用于特定目录,请执行以下操作:

    sed -i.bak '/^condition:/s/$/ \& s(new_job_dependency)/' your_dir/files*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多