0

我有一些文件如下所示。如果该列为空白,我需要在第五列中添加一个字符“C”。我更喜欢就地编辑。

1   87 E P              0   0  131      0, 0.0     0, 0.0     0, 0.0     0, 0.0   0.000 360.0 360.0 360.0 150.0    7.2   83.8   79.2
2   88 E V        +     0   0  136      1,-0.1     2,-0.5     3,-0.0     0, 0.0   0.993 360.0  80.8 -61.8 -61.8    7.7   80.9   76.8
3   89 E K  S    S+     0   0  195      2,-0.0     2,-0.3     0, 0.0    -1,-0.1  -0.222  77.4 108.3 -45.5  95.3    5.5   82.5   74.2
4   90 E R  S    S-     0   0  153     -2,-0.5     2,-0.5     2,-0.0     0, 0.0  -0.864  72.2-119.8-173.3 140.8    8.2   84.8   72.9
5  191 E R       -     0   0  202     -2,-0.3     2,-0.2     1,-0.0    -2,-0.0  -0.772  46.1-115.0 -83.4 130.6   10.4   85.4   70.0
6   92 E L  H     -     0   0  109     -2,-0.5     2,-0.5     1,-0.1    -1,-0.0  -0.499  24.5-142.8 -70.2 134.3   14.0   85.3   71.3
7   93 E D        +     0   0  126     -2,-0.2    -1,-0.1     1,-0.1     0, 0.0  -0.852  36.1 149.0-101.0 126.6   15.9   88.6   71.1
8   94 E L              0   0  140     -2,-0.5    -1,-0.1     0, 0.0    -2,-0.0   0.735 360.0 360.0-125.7 -35.5   19.6   88.3   70.3
9   95 E E              0   0  235      0, 0.0    -2,-0.0     0, 0.0     0, 0.0   0.494 360.0 360.0  -8.6 360.0   21.0   91.3   68.3

期望的输出

1   87 E P  C           0   0  131      0, 0.0     0, 0.0     0, 0.0     0, 0.0   0.000 360.0 360.0 360.0 150.0    7.2   83.8   79.2
2   88 E V  C      +    0   0  136      1,-0.1     2,-0.5     3,-0.0     0, 0.0   0.993 360.0  80.8 -61.8 -61.8    7.7   80.9   76.8
3   89 E K  S    S+     0   0  195      2,-0.0     2,-0.3     0, 0.0    -1,-0.1  -0.222  77.4 108.3 -45.5  95.3    5.5   82.5   74.2
4   90 E R  S    S-     0   0  153     -2,-0.5     2,-0.5     2,-0.0     0, 0.0  -0.864  72.2-119.8-173.3 140.8    8.2   84.8   72.9
5  191 E R  C     -     0   0  202     -2,-0.3     2,-0.2     1,-0.0    -2,-0.0  -0.772  46.1-115.0 -83.4 130.6   10.4   85.4   70.0
6   92 E L  H     -     0   0  109     -2,-0.5     2,-0.5     1,-0.1    -1,-0.0  -0.499  24.5-142.8 -70.2 134.3   14.0   85.3   71.3
7   93 E D  C      +    0   0  126     -2,-0.2    -1,-0.1     1,-0.1     0, 0.0  -0.852  36.1 149.0-101.0 126.6   15.9   88.6   71.1
8   94 E L  C           0   0  140     -2,-0.5    -1,-0.1     0, 0.0    -2,-0.0   0.735 360.0 360.0-125.7 -35.5   19.6   88.3   70.3
9   95 E E  C           0   0  235      0, 0.0    -2,-0.0     0, 0.0     0, 0.0   0.494 360.0 360.0  -8.6 360.0   21.0   91.3   68.3
4

3 回答 3

2

从您的示例中,您如何定义“第五列”并不明显。如果您的列是固定宽度,则“第五列”似乎与第十三个字符位置相对应。

如果这是正确的,那么在任何以 12 个字符和一个空格开头的行上,用同样的 12 个字符和一个 C 替换。

sed -i 's/^\(............\) /\1C/' filename

例如,如果您在 OSX 上,则需要,-i ''因为该-i选项需要该平台上的强制参数。如果您完全sed没有-i,请尝试等效的 Perl 脚本:

perl -pi -e 's/^(.{12}) /$1C/' filename

如果您的数据始终不是固定宽度,则似乎无法推断出要替换的确切内容,除非您可以更详细地制定数据描述。

于 2013-08-26T03:31:41.033 回答
1

使用

use strict; use warnings;
while (<>) {
 my @f = split(/,/);
 my @v=split(/(\s+)/,$f[0]); 
 $v[7] = "  C     " if @v < 19; 
 $v[7].= "      "   if @v < 17; 
 $f[0] = join("", @v);
 print  join(",", @f);
}

执行:

perl script.pl input

输出:

1   87 E P  C           0   0  131      0, 0.0     0, 0.0     0, 0.0     0, 0.0   0.000 360.0 360.0 360.0 150.0    7.2   83.8   79.2
2   88 E V  C     +     0   0  136      1,-0.1     2,-0.5     3,-0.0     0, 0.0   0.993 360.0  80.8 -61.8 -61.8    7.7   80.9   76.8
3   89 E K  S    S+     0   0  195      2,-0.0     2,-0.3     0, 0.0    -1,-0.1  -0.222  77.4 108.3 -45.5  95.3    5.5   82.5   74.2
4   90 E R  S    S-     0   0  153     -2,-0.5     2,-0.5     2,-0.0     0, 0.0  -0.864  72.2-119.8-173.3 140.8    8.2   84.8   72.9
5  191 E R  C     -     0   0  202     -2,-0.3     2,-0.2     1,-0.0    -2,-0.0  -0.772  46.1-115.0 -83.4 130.6   10.4   85.4   70.0
6   92 E L  H     -     0   0  109     -2,-0.5     2,-0.5     1,-0.1    -1,-0.0  -0.499  24.5-142.8 -70.2 134.3   14.0   85.3   71.3
7   93 E D  C     +     0   0  126     -2,-0.2    -1,-0.1     1,-0.1     0, 0.0  -0.852  36.1 149.0-101.0 126.6   15.9   88.6   71.1
8   94 E L  C           0   0  140     -2,-0.5    -1,-0.1     0, 0.0    -2,-0.0   0.735 360.0 360.0-125.7 -35.5   19.6   88.3   70.3
9   95 E E  C           0   0  235      0, 0.0    -2,-0.0     0, 0.0     0, 0.0   0.494 360.0 360.0  -8.6 360.0   21.0   91.3   68.3
于 2013-08-26T05:21:42.297 回答
1
awk '{a=substr($0, 17, 1);a=(a~/[[:alpha:]]/)?a:"C";print substr($0,1, 16) a substr($0,18)}' file
于 2013-08-26T05:24:18.583 回答