1

我需要一个 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.

任何人都可以帮助我开始使用这样的脚本吗?

4

3 回答 3

5

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

awk '{c += sub(/[^=]+=/,"cuts["c+0"]=") }1' file
于 2012-11-22T17:00:49.187 回答
1

在纯 bash 中:

counts=0
while IFS= read -r line; do
    if [[ "$line" =~ "=" ]]; then
        echo "${counts}${line#*=}"
        counts=$((counts+1))
    fi
done <infile

请注意,这将排除“=”。如有必要,您可以将其重新包含在 ehco 语句中。

于 2012-11-22T17:03:10.013 回答
0

这是 perl one liner:

perl -plne 'if($.==1){$count=1}if(/=/){$_=~s/[^\=]*[=]/cut[$count]/g;$count++}' temp
于 2012-11-23T06:10:57.240 回答