我有一个管道分隔文件(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|4The 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.001Print last field:
awk 'BEGIN {RS="|"}; END {print ($(NF))}' file.001Could 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 fileGiven 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 }' fileThe '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' fileIf you have the latest gawk, you can use 'in-place' editing:
gawk -i inplace '...' fileNote that this is the same as:
gawk '...' file > file.tmp && mv file.tmp file更多推荐
发布评论