修改文本文件最后一行中的特定字段 [英] Modify specific field in the last line of a text file

查看:60
本文介绍了修改文本文件最后一行中的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定是否可以执行一个快速的Liner sed或awk脚本,以修改文本文件中的某个值,特别是文件最后一行中的值.

I'm trying to figure out whether there's a quick one liner sed or awk script that I can execute in order to modify a certain value in a text file, specifically the value in the last line of the file.

当前,我的文件中包含带有数据行数的预告片行.我想修改它,使其包括包括页眉和页脚在内的计数.任何帮助将不胜感激.

Currently my file has a trailer line with count of data lines. I want to modify this so that it includes the count including header and footer. Any help would be much appreciated.

file1代码:

H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|38|1208004|1
T|3

修改后,输出应为

H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|38|1208004|1
T|5

推荐答案

修改以T开头的行:

$ awk '{sub(/^T.*/,"T|"NR)}1' file
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|38|1208004|1
T|5

要按照原始要求修改输入文件的最后一行:

To modify the last line of your input file as originally requested:

$ awk '{printf "%s",p} {p=$0 ORS} END{sub(/\|.*/,"|"NR,p); print p}' file
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|38|1208004|1
T|5

由于其评论中存在一些辩论,原因是我为何对此处发布的getline解决方案不满意在评论中举一些例子-这是为什么您不应该使用getline解决方案(或类似的解决方案)解决此问题(或类似的问题)的几个示例:

Since there was some debate in its comments about why I downvoted a getline solution posted here and since it's difficult to give examples in comments - here's a couple of examples of why you should not use that getline solution (or any like it) for this problem (or any like it):

适用于一组输入:

$ cat file1
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|28|1208004|1
T|3

$ awk '{printf "%s",p} {p=$0 ORS} END{sub(/\|.*/,"|"NR,p); print p}' file1
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|28|1208004|1
T|5

$ awk '{l=$0; if(getline==1){print l; print} else {sub("\\|.*","|"NR);print}}' file1
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
D|849007|28|1208004|1
T|5

另一个失败:

$ cat file2
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
T|3

$ awk '{printf "%s",p} {p=$0 ORS} END{sub(/\|.*/,"|"NR,p); print p}' file2
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
T|4

$ awk '{l=$0; if(getline==1){print l; print} else {sub("\\|.*","|"NR);print}}' file2
H|ACCT|XEC|1|TEMP|20130215035845|
D|849002|48|1208004|1
D|849007|28|1208004|1
T|3

尴尬(充其量)以增强最小的工作效率,例如将每行打印到stderr进行调试:

Awkward (at best) to enhance for the smallest job, e.g. printing each line to stderr for debugging:

$ awk '{print |"cat>&2"} {printf "%s",p} {p=$0 ORS} END{sub(/\|.*/,"|"NR,p); print p}' file2

$ awk '{print |"cat>&2"; l=$0; if(getline==1){print |"cat>&2"; print l; print} else {print |"cat>&2"; sub("\\|.*","|"NR); print}}' file1

请注意,修改2个版本之间的简单性有所不同.修改getline版本笨拙,复杂,不平凡,不明显,效率低下,容易出现隐患,需要重复代码和/或大量重写等...

Notice the difference in simplicity between modifying the 2 versions. Modifying the getline version is clumsy, complicated, non-trivial, non-obvious, inefficient, open to insidious errors, needing duplicated code and/or significant re-write, etc...

我们在上面看到的是尝试使用getline解决awk的自然文本处理模式可以轻松处理的问题的非常常见的后果.

What we see above are the VERY common repercussions of trying to use getline to solve problems that awk's natural text processing mode can easily handle.

getline在正确使用时很有用,请参见 http://awk.info/?tip/getline 有关有效应用程序的一些示例.

getline is useful when used appropriately, see http://awk.info/?tip/getline for some examples of valid applications.

这篇关于修改文本文件最后一行中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆