1

以下のようなテキスト ファイルがあります^|^が特定の文字列 (inこの例は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

シェル スクリプトを使用してこのジョブを実行するにはどうすればよいですか?

4

6 に答える 6

3

使用する1つの方法は次の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が得意とする1行での単純な置換であるため、投稿されたsedソリューションの1つを使用します。

于 2012-11-17T18:30:39.817 に答える
0

新しい文字列で大文字を使用するには:

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

sedコマンドが実行される行のフィルターである「アドレス」を指定できます。

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

この場合、アドレスは/^MMX/、コマンドはで、s///gに置き換え\^(.*)\|られます。 は括弧内の部分です。^\1^\1|\1

于 2012-11-17T06:31:10.583 に答える
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 に答える
-2

sed s/^MMX([^^] )^([^|] )\|(.+)/MMX\1^\2^\2\|\3/ ファイル名

于 2012-11-17T05:38:26.473 に答える