[英]How to remove several columns and the field separators at once in AWK?
I have a big file with several thousands of columns. 我有一个包含数千列的大文件。 I want to delete some specific columns and the field separators at once with AWK in Bash. 我想在Bash中使用AWK一次删除一些特定的列和字段分隔符。
I can delete one column at a time with this oneliner (column 3 will be deleted and its corresponding field separator): 我可以使用此oneliner一次删除一列(第3列将被删除,其对应的字段分隔符):
awk -vkf=3 -vFS="\t" -vOFS="\t" '{for(i=kf; i<NF;i++){ $i=$(i+1);}; NF--; print}' < Big_File
However, I want to delete several columns at once... Can someone help me figure this out? 但是,我想一次删除几个列...有人可以帮我解决这个问题吗?
You can pass list of columns to be deleted from shell to awk
like this: 您可以将要从shell中删除的列的列表传递给awk
如下所示:
awk -vkf="3,5,11" ...
then in the awk
programm parse it into array: 然后在awk
程序中将其解析为数组:
split(kf,kf_array,",")
and then go thru all the colums and test if each particular column is in the kf_array and possibly skip it 然后通过所有列并测试每个特定列是否在kf_array中并可能跳过它
Other possibility is to call your oneliner several times :-) 其他可能性是多次打电话给你的oneliner :-)
Here is an implementation of Kamil's idea: 以下是卡米尔的想法的实现:
awk -v remove="3,8,5" '
BEGIN {
OFS=FS="\t"
split(remove,a,",")
for (i in a) b[a[i]]=1
}
{
j=1
for (i=1;i<=NF;++i) {
if (!(i in b)) {
$j=$i
++j
}
}
NF=j-1
print
}
'
If you can use cut
instead of awk
, this one is easier with cut
: 如果你可以使用cut
而不是awk
,这个更容易cut
:
eg this obtains columns 1,3, and from 50 on from file: 例如,这会从文件中获取列1,3和50:
cut -f1,3,50- file
Something like this should work: 这样的事情应该有效:
awk -F'\t' -v remove='3|8|5' '
{
rec=ofs=""
for (i=1;i<=NF;i++) {
if (i !~ "^(" remove ")$" ) {
rec = rec ofs $i
ofs = FS
}
}
print rec
}
' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.