用不同的值替换第一次和第二次出现的相同文本

发布于 2024-10-31 09:20:32 字数 474 浏览 6 评论 0原文

我正在寻找一种方法,将文本文件中某个文本的第一次出现替换为值 ${A},并将同一文本在不同行上的第二次出现替换为 ${B}。这可以使用 sed 或 awk 或任何其他 UNIX 工具来实现吗?

工具集相当有限:bash、常见的 UNIX 工具,如 sed、grep、awk 等。不能使用 Perl、Python、Ruby 等...

提前感谢您的任何建议 罗伯特

示例:

...
Text
Text
Text
Text
TEXT_TO_BE_REPLACED
Text
Text
Text
TEXT_TO_BE_REPLACED
Text
Text
Text
...

应替换为

...
Text
Text
Text
Text
REPLACEMENT_TEXT_A
Text
Text
Text
REPLACEMENT_TEXT_B
Text
Text
Text
...

I'm searching for a way to replace the first occurrence of a certain text in a text file with a value ${A} and the second occurrence of the same text, on a different line, with ${B}. Can this be achieved with sed or awk or any other UNIX tool?

The toolset is fairly limited: bash, common UNIX tools like sed, grep, awk etc. Perl, Python, Ruby etc. cannot be used...

Thanks in advance for any advice
Robert

Example:

...
Text
Text
Text
Text
TEXT_TO_BE_REPLACED
Text
Text
Text
TEXT_TO_BE_REPLACED
Text
Text
Text
...

should be replaced with

...
Text
Text
Text
Text
REPLACEMENT_TEXT_A
Text
Text
Text
REPLACEMENT_TEXT_B
Text
Text
Text
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你的呼吸 2024-11-07 09:20:32

sed 运行一次:


sed -e 's/\(TEXT_TO_BE_REPLACED\)/REPLACEMENT_TEXT_A/1' \
    -e 's/\(TEXT_TO_BE_REPLACED\)/REPLACEMENT_B/1' < input_file > output_file

Sed with one run:


sed -e 's/\(TEXT_TO_BE_REPLACED\)/REPLACEMENT_TEXT_A/1' \
    -e 's/\(TEXT_TO_BE_REPLACED\)/REPLACEMENT_B/1' < input_file > output_file
此生挚爱伱 2024-11-07 09:20:32

只需运行脚本两次 - 一次用 ${A} 替换第一次出现,一次用 ${B} 替换(现在第一次)出现。
仅替换一次发生:(

sed '0,/RE/s//to_that/' file

无耻地从 如何使用 sed 只替换文件中的第一个匹配项?)

Just run your script twice - once to replace the first occurrence with ${A}, once to replace the (now first) occurence with ${B}.
To replace just one occurence:

sed '0,/RE/s//to_that/' file

(shamelessly stolen from How to use sed to replace only the first occurrence in a file?)

赴月观长安 2024-11-07 09:20:32

这是使用 awk 的可能解决方案:

#!/usr/bin/awk -f

/TEXT_TO_BE_REPLACED/ {
   if ( n == 0 ) {
      sub( /TEXT_TO_BE_REPLACED/, "REPLACEMENT_TEXT_A", $0 );
      n++;
   }
   else if ( n == 1 ) {
      sub( /TEXT_TO_BE_REPLACED/, "REPLACEMENT_TEXT_B", $0 );
      n++;
   }
}

{
   print
}

Here is a possible solution using awk:

#!/usr/bin/awk -f

/TEXT_TO_BE_REPLACED/ {
   if ( n == 0 ) {
      sub( /TEXT_TO_BE_REPLACED/, "REPLACEMENT_TEXT_A", $0 );
      n++;
   }
   else if ( n == 1 ) {
      sub( /TEXT_TO_BE_REPLACED/, "REPLACEMENT_TEXT_B", $0 );
      n++;
   }
}

{
   print
}
莫相离 2024-11-07 09:20:32
awk 'BEGIN { a[0]="REPLACEMENT_A"; a[1]="REPLACEMENT_B"; } \
  /TEXT_TO_BE_REPLACED/ { gsub( "TEXT_TO_BE_REPLACED", a[i++]); i%=2 }; 1'
awk 'BEGIN { a[0]="REPLACEMENT_A"; a[1]="REPLACEMENT_B"; } \
  /TEXT_TO_BE_REPLACED/ { gsub( "TEXT_TO_BE_REPLACED", a[i++]); i%=2 }; 1'
感情洁癖 2024-11-07 09:20:32

因此,您可以使用 sed 来执行此操作,如下所示:

首先,我创建了一个名为 test.txt 的文件,其中包含:

well here is an example text example
and here is another example text

我选择使用单词“example”作为要更改的值。

这是命令:cat test.txt | sed -e 's/(example)/test2/2' -e 's/(example)/test1/1'

提供以下输出:

well here is an test1 text test2
and here is another test1 text

现在 sed 命令被分解:

s - begins search + replace 
/ - start search ended with another /
The parentheses group our text ie example
/test2/ what we are putting in place of example
The number after the slashes is the occurrence we want to replace. 

the -e allows you to run both commands on one command line.

So, you can use sed to do this like so:

First, I made a file named test.txt that contained:

well here is an example text example
and here is another example text

I choose to use the word "example" to be the value to change.

Here is the command: cat test.txt | sed -e 's/(example)/test2/2' -e 's/(example)/test1/1'

which provides the following output:

well here is an test1 text test2
and here is another test1 text

Now the sed command broken down:

s - begins search + replace 
/ - start search ended with another /
The parentheses group our text ie example
/test2/ what we are putting in place of example
The number after the slashes is the occurrence we want to replace. 

the -e allows you to run both commands on one command line.
只涨不跌 2024-11-07 09:20:32

您还可以使用文本编辑器 ed:

# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s file
   H
   /TEXT_TO_BE_REPLACED/s//REPLACEMENT_TEXT_A/
   /TEXT_TO_BE_REPLACED/s//REPLACEMENT_TEXT_B/
   wq
EOF

You may also use the text editor ed:

# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s file
   H
   /TEXT_TO_BE_REPLACED/s//REPLACEMENT_TEXT_A/
   /TEXT_TO_BE_REPLACED/s//REPLACEMENT_TEXT_B/
   wq
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文