1

我有一个如下所示的文本文件,如果^|^特定字符串开头(在这个例子是MMX

文本文件原文:

General start, this is a test file.
TAG okay, this line not need to be processed.
MMX ABCD ^string1|other strings abc
CCF ABCD ^string2|other strings cde, skip line
MMX CDEE ^String3|other strings aaa
MMX AAAA ^String4|other strings bbb
CCD BBBB ^String5|other strings ccc, skip line

修改后的文本文件应该是:

General start, this is a test file.
TAG okay, this line not need to be processed.
MMX ABCD ^string1^String1|other strings abc
CCF ABCD ^string2|other strings cde, skip line
MMX CDEE ^String3^String3|other strings aaa
MMX AAAA ^String4^String4|other strings bbb
CCD BBBB ^String5|other strings ccc, skip line

如何使用 shell 脚本来执行这项工作?

4

6 回答 6

3

这是一种使用方法sed

sed '/^MMX/s/\(\^[^|]*\)/\1\1/' file.txt

结果:

General start, this is a test file.
TAG okay, this line not need to be processed.
MMX ABCD ^string1^string1|other strings abc
CCF ABCD ^string2|other strings cde, skip line
MMX CDEE ^String3^String3|other strings aaa
MMX AAAA ^String4^String4|other strings bbb
CCD BBBB ^String5|other strings ccc, skip line
于 2012-11-17T06:29:32.790 回答
1

只是为了完整性:

$ awk '/^MMX/{sub(/\^[^|]+/,"&&")}1' file
General start, this is a test file.
TAG okay, this line not need to be processed.
MMX ABCD ^string1^string1|other strings abc
CCF ABCD ^string2|other strings cde, skip line
MMX CDEE ^String3^String3|other strings aaa
MMX AAAA ^String4^String4|other strings bbb
CCD BBBB ^String5|other strings ccc, skip line

但我会使用已发布的 sed 解决方案之一,因为这是单行的简单替换,这是 sed 擅长的。

于 2012-11-17T18:30:39.817 回答
0
perl -plne "if(/^MMX/){$_=~s/([^\^]*)([^\|]*)(.*)/$1$2$2$3/g;}" your_file

测试如下:

>perl -plne "if(/^MMX/){$_=~s/([^\^]*)([^\|]*)(.*)/$1$2$2$3/g;}" new.txt
General start, this is a test file.
TAG okay, this line not need to be processed.
MMX ABCD ^string1^string1|other strings abc
CCF ABCD ^string2|other strings cde, skip line
MMX CDEE ^String3^String3|other strings aaa
MMX AAAA ^String4^String4|other strings bbb
CCD BBBB ^String5|other strings ccc, skip line
于 2012-11-17T08:45:37.783 回答
0

您可以提供sed一个“地址”,它是执行命令的行的过滤器:

sed '/^MMX/s/\^(.*)\|/^\1^\1|/g'

在这种情况下,地址是/^MMX/,命令是s///g,并且它替换\^(.*)\|^\1^\1|,其中\1括号中的部分是。

于 2012-11-17T06:31:10.583 回答
0

要确保新字符串中的大写:

sed '/^MMX/s/\^\([^|]\+\)/^\1^\u\1/'
于 2012-11-17T11:13:55.123 回答
-2

sed s/^MMX([^^] )^([^|] )\|(.+)/MMX\1^\2^\2\|\3/ 文件名

于 2012-11-17T05:38:26.473 回答