1

我正在尝试将 2 个功能合并到我的awk命令中。我想要tolower第 2 列中 Col1 中的信息(因此 Col1 中的信息将是 2 cols 的值 - Col1 和 Col2,其中的值在lowerCol2 中),我想从1-N开始和结束我拥有的某些标记的开始。

数据(制表符分隔)当前如下所示:

<s>
He  PRP -
could   MD  -
tell    VB  -
she PRP -
was VBD -
teasing VBG -
him PRP -
.   .   .
</s>
<s>
He  PRP -
kept    VBD -
his PRP$    -
eyes    NNS -
closed  VBD -
,   ,   -
but CC  -
he  PRP -
could   MD  -
feel    VB  -
himself PRP -
smiling VBG -
.   .   .
</s>

理想的输出是这样的:

<s>
He  he  PRP 1
could   could   MD  2
tell    tell    VB  3
she     she PRP 4
was was VBD     5
teasing teasing VBG 6
him him PRP 7
.   .   .   8
</s>
<s>
He  he  PRP 1-
kept    kept    VBD 2
his his PRP$    3
eyes    eyes    NNS 4
closed  closed  VBD 5
,   ,   ,   6
but but CC  7
he  he  PRP 8
could   could   MD  9
feel    feel    VB  10
himself     himself PRP 11
smiling smiling VBG 12
.   .   .   13
</s>

awk我正在尝试但不起作用的两步是:

第1步:

awk '!NF{$0=x}1' input | awk '{$1=$1; print "<s>\n" $0 "\t.\n</s>"}' RS=  FS='\n' OFS='\t-\n' > output

这里,不知道怎么把“-”变成计数器

和第2步(直接给我一个错误):

awk '{print $1 "\t" '$1 = tolower($1)' "\t" $2 "\t" $3}' input > output

任何建议1.关于如何解决lower和counter和2.是否可以将这两个步骤结合起来?

先感谢您

4

1 回答 1

3

我会做类似的事情:

$ awk 'BEGIN{FS=OFS="\t"} NF>1{$1=$1 FS tolower($1); $4=++f} NF==1{f=0}1' file
<s>
He he PRP - 1
could could MD - 2
tell tell VB - 3
she she PRP - 4
was was VBD - 5
teasing teasing VBG - 6
him him PRP - 7
. . . . 8
</s>
<s>
He he PRP - 1
kept kept VBD - 2
his his PRP$ - 3
eyes eyes NNS - 4
closed closed VBD - 5
, , , - 6
but but CC - 7
he he PRP - 8
could could MD - 9
feel feel VB - 10
himself himself PRP - 11
smiling smiling VBG - 12
. . . . 13
</s>

也就是说,设置$1$4没有<s>行,否则重置计数器(是的,我知道它正在重置两次,但我现在想不出更整洁的东西)。然后1就可以正常打印了。

请注意,您经常使用print和分隔符。最好只更改字段并print在 True 条件 ( 1) 并使用给定的字段分隔符时自动发生。一种模型视图控制器:)

于 2015-10-30T11:32:53.870 回答