如何追加在一个VI文件中的每一行的末尾一些命令的输出? [英] How to append some command output at the end of each line in a Vi file?

查看:119
本文介绍了如何追加在一个VI文件中的每一行的末尾一些命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个VI文件如下:

猫文件1

  ABC 123 PQR
LMN 234 RST
JKL 100星期一

我想利用每一行的第二场(即,在这种情况下是123,234和100),并将其附加到该同一行的结尾。
我该怎么办?

输出应如下所示:

  ABC 123 PQR 123
LMN 234 RST 234
JKL 100周一100


解决方案

通过 AWK

  $的awk'{NF = NF + 1; $ NF = $ 2} 1'文件
ABC 123 PQR 123
LMN 234 RST 234
JKL 100周一100

有递增字段的一个数量和设置最后一个作为第2位。然后 1 是一个真正的条件,这被评估为默认的awk行为: {打印$ 0}

或者也

 的awk'{$打印0,$ 2}'文​​件

它输出全线加上第二场。

甚至更短,<一个href=\"http://stackoverflow.com/questions/21624505/how-to-append-some-command-output-at-the-end-of-each-line-in-a-vi-file/21624605?noredirect=1#comment32677309_21624605\">thanks !哈康Hægland:

 的awk'{$(NF + 1)= $ 2} 1'文件

Suppose I have a vi file as the following:

cat file1

abc 123 pqr
lmn 234 rst
jkl 100 mon

I want to take the 2nd field of each line (viz, in this case is 123, 234 and 100) and append it to the end of that same line. How will I do that?

The output should look like the following:

abc 123 pqr 123
lmn 234 rst 234
jkl 100 mon 100

解决方案

With awk:

$ awk '{NF=NF+1; $NF=$2}1' file
abc 123 pqr 123
lmn 234 rst 234
jkl 100 mon 100

It increments the number of field in one and sets the last one as the 2nd. Then 1 is a true condition, which is evaluated as the default awk behaviour: {print $0}.

Or also

awk '{print $0, $2}' file

It prints the full line plus the second field.

Or even shorter, thanks Håkon Hægland!:

awk '{$(NF+1)=$2}1' file

这篇关于如何追加在一个VI文件中的每一行的末尾一些命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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