我对shell脚本非常缺乏经验,当名为Views的列包含值0时,我需要编写一个删除整行的列。" Views"可能并不总是在文件中的相同位置,所以我需要一些方法来预先找到列的位置。这是sed还是awk可行的东西?或者还有其他我可以使用的东西吗?
谢谢!
答案 0 :(得分:3)
使用awk,可以这样做:
awk -F, 'NR == 1 { for(i = 1; i <= NF; ++i) { col[$i] = i }; next } $col["Views"] != 0' filename.csv
-F,
将字段分隔符设置为逗号,因为您提到了CSV文件。
代码是
NR == 1 { # in the first line
for(i = 1; i <= NF; ++i) { # go through all fields
col[$i] = i # remember their index by name.
# ($i is the ith field)
}
next # and do nothing else
}
$col["Views"] != 0 # after that, select lines in which the field in
# the column that was titled "Views" is not zero,
# and do the default action on them (i.e., print)
请注意,这只会过滤掉Views
列正好为0的行。如果您还要过滤掉Views
字段为空的行,请使用$col["Views"]
代替$col["Views"] != 0
。
答案 1 :(得分:0)
awk -F ',' 'NR==1{print;for(i=1;i<=NF;++i){if($i=="Views"){x=$i;y=i}}};NR>1{if($y!=0){print}}' file > new_file
代码细分
NR==1{ #for the first line
print #print it
for(i=1;i<=NF;++i){ #make a loop to read all the column and find the
if($i=="Views"){ #name "Views" in the first row.
y=i #Save the column number in a variable named y
}
}
}
NR>1{ # start from line 2 going downwards targeting
if($y!=0){ # the Views Column
print #if it does not contain 0, print the line
}
}
答案 2 :(得分:0)
awk '($1 == "badString") && !($1 ~ /[.]/) { next } 1' inputfile > outputfile
#if第一列= badString或has。 (点)不要将其包含在输出文件中