使用行计数值(awk?)更新最后一个字段(Update last field with value from line count (awk?))

编程入门 行业动态 更新时间:2024-10-13 14:22:26
使用行计数值(awk?)更新最后一个字段(Update last field with value from line count (awk?))

我有一个管道分隔文件(file.001)如下:

00|FIELD10|FIELD02 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 99|4

以'01'开头的行是记录(00 =标题,99 =预告片)。 预告片中的最后一个字段(当前填充“4”)是记录计数,但是此记录计数并不总是准确的。

我想要做的是计算记录数量并更新记录计数字段,同时保持其余数据不变(打印到新文件会很好,理想情况下它将在同一个文件中)。 我对此的解释是将计数值打印到最后一个字段值,但我不知道如何做到这一点。

我一直用awk尝试这个,目前有以下打印计数和最后一个字段:

打印记录数:

awk '/^01/ {count++} END {print count }' file.001

打印最后一栏:

awk 'BEGIN {RS="|"}; END {print ($(NF))}' file.001

谁能提出如何做到这一点的建议? 如果我到目前为止所拥有的内容不是很好的话,我很新闻。 我也愿意使用awk以外的东西来实现这一目标。

I have a pipe delimited file (file.001) as below:

00|FIELD10|FIELD02 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 01|FIELD01|FIELD02|FIELD03 99|4

The lines beginning with '01' are the records (00 = header, 99 = trailer). The last field in the trailer (currently populated with '4') is the record count, however this record count is not always accurate.

What I want to do is count the number of records and update the record count field while leaving the rest of the data untouched (printing to a new file would be fine, ideally it would be within the same file). My interpretation of this would be to print the count value into the last field value but I don't know how to do this.

I have been attempting this with awk and currently have the following for printing the count and the last field:

Print record count:

awk '/^01/ {count++} END {print count }' file.001

Print last field:

awk 'BEGIN {RS="|"}; END {print ($(NF))}' file.001

Could anyone offer a suggestion on how to do this? I am new to awk so apologies if what I have above so far isn't very good. I am also open to using something other than awk to achieve this.

最满意答案

鉴于你的第一个awk声明,我很惊讶你没有使用这样的东西来打印最后一个字段(记录数):

awk -F '|' '/^99/ { print $NF }' file

'预告片'总是以'99'开头,对吗? 因此,您可以使用它来对文件进行所需的更改。 也许尝试以下方法:

awk 'BEGIN { OFS=FS="|" } $1 == "01" { c++ } $1 == "99" { $2 = c }1' file

如果你有最新的gawk ,你可以使用'就地'编辑:

gawk -i inplace '...' file

请注意,这与:

gawk '...' file > file.tmp && mv file.tmp file

Given your first awk statement, I'm surprised you didn't use something like this to print the last field (count of records):

awk -F '|' '/^99/ { print $NF }' file

The 'trailer' always begins with '99', correct? So you can use this to make the desired changes to your file. Perhaps try the following:

awk 'BEGIN { OFS=FS="|" } $1 == "01" { c++ } $1 == "99" { $2 = c }1' file

If you have the latest gawk, you can use 'in-place' editing:

gawk -i inplace '...' file

Note that this is the same as:

gawk '...' file > file.tmp && mv file.tmp file

更多推荐

本文发布于:2023-08-06 18:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   数值   awk   Update   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!