如果该列为空,则在列中添加字符C.

时间:2013-08-26 03:19:48

标签: bash sed awk

我有一些文件如下所示。如果该列为空,我需要在第五列中添加字符“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

3 个答案:

答案 0 :(得分:2)

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

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

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

如果你是在OSX上,您需要-i '',因为-i选项需要该平台上的必需参数。如果您的sed完全缺少-i,请尝试使用等效的Perl脚本:

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

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

答案 1 :(得分: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

答案 2 :(得分:1)

awk '{a=substr($0, 17, 1);a=(a~/[[:alpha:]]/)?a:"C";print substr($0,1, 16) a substr($0,18)}' file