简体   繁体   English

按某些列过滤文本文件行,不带引号

[英]filter text file rows by certain column without quotation marks

so I have a file that I'd like to only get certain rows from, but unfortunately the strings within those rows aren't surrounded by quotation marks. 所以我有一个文件,我只想从中获取某些行,但是不幸的是,这些行中的字符串没有用引号引起来。 How can I filter these rows based on the 2nd column value?? 如何根据第二列的值过滤这些行? Eg I have: 例如,我有:

string a,string b,string c,string d,string e
string 1,string 2,string 3,string 4,string 5
string f,string b,string h,string i,string j
string 6,string 7,string 8,string 9,string 0

And I want in a new file only: 我只想要一个新文件:

string a,string b,string c,string d,string e
string f,string b,string h,string i,string j

I'm trying to use the command: 我正在尝试使用以下命令:

awk -F '","'  'BEGIN {OFS=","} { if (toupper($2) == "STRING B")  print }' input.csv > output.csv

Your code should work if you use -F, instead of -F '","' . 如果使用-F,而不是-F '","' -F,则您的代码应该可以工作。 A somewhat more idiomatic way to do the same is 一种更惯用的方法是

awk -F, -v OFS=, 'toupper($2) == "STRING B"' input.csv

In awk code, a condition without an associated action means to perform the default action (printing) if the condition is true, so this comes to the same effect with less fluff. 在awk代码中,没有关联动作的条件表示如果条件为真,则执行默认操作(打印),因此达到相同的效果且毛发较少。

You don't need to include double quotes on setting the FS value. 设置FS值时,无需包含双引号。

$ awk -F,  'BEGIN {OFS=","} { if (toupper($2) == "STRING B")  print }' file
string a,string b,string c,string d,string e
string f,string b,string h,string i,string j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM