【问题标题】:Linux Replace a particular string with consecutive numbers [closed]Linux用连续数字替换特定字符串[关闭]
【发布时间】:2019-11-01 14:22:43
【问题描述】:
CONFIG.CONFIG
create state file${number_of_edges} {
 {interval=60 
idle_interval_notification="DISABLED"}    
}
create state file${number_of_edges} {
 {interval=60 
idle_interval_notification="DISABLED"}    
}

我有文件,其内容如上。我想用 0,1,2 等连续数字替换 ${number_of_edges} 字符串

EXPECTED OUTPUT
create state file0 {
 {interval=60 
idle_interval_notification="DISABLED"}    
}
create state file1{
 {interval=60 
idle_interval_notification="DISABLED"}    
}
create state file2 {
 {interval=60 
idle_interval_notification="DISABLED"}    
}

【问题讨论】:

标签: regex linux bash awk sed


【解决方案1】:

我得到了解决方案。

命令 -

awk -vRS=edge '{$0=n$0;ORS=RT}++n' FILE

【讨论】:

  • 恕我直言,也许您可​​以将此代码添加到您的帖子中。您确定此命令提供与您的要求相同的预期输出吗?当我运行它时,它提供的结果与您的示例预期结果不同。
【解决方案2】:

一个简单的awk 脚本:

awk 'sub("\\${number_of_edges}",cnt+1, $0){cnt++}1' input.txt

说明:

sub("\\${number_of_edges}",cnt+1, $0) 对于每个输入行,将 ${number_of_edges} 替换为变量 (cnt + 1)

{cnt++}如果替换大于0,则递增变量cnt

1输出每一行输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多