仅将函数应用于具有可变位置的一列 [英] Apply a function to one column only, with a variable location
问题描述
我有一堆不同的文件.所有文件都包含一个标题为ID的列,但不一定在所有文件中的同一位置.我有一个功能要应用于所有文件中的ID,以将其更改为NEWID.
I have a bunch of different files. All files contain a column titled ID, but not necessarily in the same location in all of the files. I have a function I want to apply to ID in all of the files to change them into NEWID.
我知道,如果我传递ID的列号,我可以很简单地做到这一点,说它是5列文件中的第3列,内容如下:
I know that if I pass in the column number of ID I can do this pretty simply, say it's the 3rd column in a 5 column file with something like:
awk -v column=$COLNUMBER '{print $1, $2, FUNCTION($column), $4, $5}' FILE
但是,如果我的所有文件都有数百列,并且每个文件中的任意位置,这都是非常繁琐的.我正在寻找一种可以按照以下方式做事的方法:
However if all of my files have hundreds of columns and it's somewhere arbitrary in each file, this is incredibly tedious. I am looking for a way to do something along the lines of this:
awk -v column=$COLNUMBER '{print #All columns before $column, FUNCTION($column), #All columns after $column}' FILE
我尝试了不同的循环,但是还没有任何工作.
I've tried different loops but haven't gotten any to work yet.
推荐答案
简单:
$ awk -v column=$COLNUMBER '{ $column = FUNCTION($column); print }' $FILE
这篇关于仅将函数应用于具有可变位置的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!