我怎样才能在AWK一个文件的第n列写? [英] How can I write in nth column of a file in awk?

查看:135
本文介绍了我怎样才能在AWK一个文件的第n列写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  ABC
XYZ
123
546

input.txt的:

  asdad
asdad
adghf
dfytr

我想在第二列中添加上述列。预期的输出是下面给出。

output.txt的:

  asdad ABC
asdad XYZ
adghf 123
dfytr 567


解决方案

粘贴是最简单的解决方案。下面是不必存储在存储器中的整个第一个文件一个awk例如:

 的awk'{函数getline第二< 例;的printf(%S \\ t%S \\ n,$ 0秒)}'input.txt中

For example:

abc
xyz
123
546

input.txt:

asdad
asdad
adghf
dfytr

I wanted to add the above column in 2nd column. The expected output is given below.

output.txt:

asdad  abc
asdad  xyz
adghf  123
dfytr  567

解决方案

paste is the easiest solution. Here's an awk example that doesn't have to store the entire first file in memory:

awk '{getline second < "example"; printf("%s\t%s\n",$0,second)}' input.txt

这篇关于我怎样才能在AWK一个文件的第n列写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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