Bash 搜索并替换为shell脚本

Bash 搜索并替换为shell脚本,bash,shell,sed,awk,Bash,Shell,Sed,Awk,我需要一个shell脚本来查找和替换如下所示的文本: For each line in a file find equation mark "=" remove everything up to the equation mark on that line and replace it with the string cuts[Counter] where Counter counts how many times such substitutions have been made.

我需要一个shell脚本来查找和替换如下所示的文本:

For each line in a file
  find equation mark "="
  remove everything up to the equation mark on that line and replace it with the string cuts[Counter] where Counter counts how many times such substitutions have been made.
有谁能帮我开始编写这样的脚本吗?

假设你的意思是“达到第一个等式标记…”并且你想保留=,那么应该这样做:

awk '{c += sub(/[^=]+=/,"cuts["c+0"]=") }1' file
假设您的意思是“直到第一个等式标记…”并且您希望保留=,则应该这样做:

awk '{c += sub(/[^=]+=/,"cuts["c+0"]=") }1' file
在纯bash中:

counts=0
while IFS= read -r line; do
    if [[ "$line" =~ "=" ]]; then
        echo "${counts}${line#*=}"
        counts=$((counts+1))
    fi
done <infile
计数=0
而IFS=读取-r行;做
如果[[“$line”=~“=”];然后
回显“${counts}${line#*=}”
计数=$((计数+1))
fi
在纯bash中完成:

counts=0
while IFS= read -r line; do
    if [[ "$line" =~ "=" ]]; then
        echo "${counts}${line#*=}"
        counts=$((counts+1))
    fi
done <infile
计数=0
而IFS=读取-r行;做
如果[[“$line”=~“=”];然后
回显“${counts}${line#*=}”
计数=$((计数+1))
fi

完成以下是perl one的一行代码:

perl -plne 'if($.==1){$count=1}if(/=/){$_=~s/[^\=]*[=]/cut[$count]/g;$count++}' temp

下面是perl one的一行代码:

perl -plne 'if($.==1){$count=1}if(/=/){$_=~s/[^\=]*[=]/cut[$count]/g;$count++}' temp

@mareks如果解决了您的问题,请不要忘记向上投票并接受此答案。@mareks如果解决了您的问题,请不要忘记向上投票并接受此答案。请注意,如果文件包含反斜杠或以空格开头或结尾的行,或包含空格链或制表符,则会损坏您的输出。当IFS=read-r行时,始终使用
,并且始终引用变量,除非您有非常具体的理由不引用。编辑以引用变量。我不明白为什么在这个实例中需要更改IFS?@cmh:如果不将IFS设置为空字符串,脚本将从输入文件中删除前导或尾随空格,如果不使用-r arg进行读取,脚本将解释“\”而不会在输出中再现它们。请在包含行
foobar\
的文件上尝试。请注意,如果文件包含反斜杠或以空格开头或结尾的行,或包含空格链或制表符,则会损坏输出。当IFS=read-r行时,始终使用
,并且始终引用变量,除非您有非常具体的理由不引用。编辑以引用变量。我不明白为什么在这个实例中需要更改IFS?@cmh:如果不将IFS设置为空字符串,脚本将从输入文件中删除前导或尾随空格,如果不使用-r arg进行读取,脚本将解释“\”而不会在输出中再现它们。在包含行
foobar\
的文件上尝试。