的Perl / awk的/桑达 - 查找和替换数字与放大器;自动递增 [英] Perl / Awk / Sed - Find and Replace Number & auto-increment

查看:284
本文介绍了的Perl / awk的/桑达 - 查找和替换数字与放大器;自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个的Perl / awk的/ SED 命令自动增量在7列中的数字
例如行文件knownfile.txt:

该文件格式和7列。

 今天是伟大的一天,它
也是明天的美好的一天,现在
blahblah今天foo的内部版本号是5928

我要保留所有格式,简单地替换 5928 5929 并保存文件

我试过:

 的awk'/ blahblah / {$ 7 ++;打印$ 0}'文件名和GT; TMP和放大器;&安培; MV TMP文件名

它增加了一些,我认为它保留了格式化,但只打印编辑行到文件中。


解决方案

  $猫文件
今天是伟大的一天,它
也是明天的美好的一天,现在
blahblah今天foo的内部版本号是5928$ AWK'{子(/ [[:数字:]] + $ /,$ NF + 1)} 1'文件
今天是伟大的一天,它
也是明天的美好的一天,现在
blahblah今天foo的内部版本号是5929

如果没有你想要再认真思考一下什么样的输入您发布作为预期的输出就是我们必须去什么。

I am looking for a perl/awk/sed command to auto-increment the number in column 7 Example line in file knownfile.txt:

The file has formatting and 7 columns.

today is a great day and it
is also an amazing day tomorrow now
blahblah foo today build number is 5928

I want to retain all formatting and simply replace 5928 with 5929 and save file

I tried:

awk '/blahblah/{ $7++; print $0 }' filename > tmp && mv tmp filename

It increments the number and I think it retains the formatting, but it only prints the edited line to the file.

解决方案

$ cat file
today is a great day and it
is also an amazing day tomorrow now
blahblah    foo   today     build number is     5928

$ awk '{sub(/[[:digit:]]+$/,$NF+1)}1' file
today is a great day and it
is also an amazing day tomorrow now
blahblah    foo   today     build number is     5929

If that doesn't do what you want then seriously THINK about what sample input and expected output you're posting as that's all we have to go on.

这篇关于的Perl / awk的/桑达 - 查找和替换数字与放大器;自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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