0

如果文件的第 3 列包含字符串“BMR”,我想删除该文件,字符串可能是 - “BMR13234”,这意味着“BMR”是该字符串的静态部分,它可以包含任何其他字符。

因此,如果第 3 列包含 -“BMR*”(* 指任何字符),则应从文件中删除整个第 3 列。

例如: 输入:

"ABC","0.20","BMR1234442","2001-01-01"
"LMN","0.00","BMR2490289","2008-01-01"
"LTD","0.20","BMR2345577","2001-01-01"

输出应为:(仅当存在 BMR 时,否则不应删除第 3 列)

"ABC","0.20","2001-01-01"
"LMN","0.00","2008-01-01"
"LTD","0.20","2001-01-01"

请建议。

4

2 回答 2

0

一种awk解决方案

awk -F, '$3~/^BMR/ {$3=x} {sub(/,,/,",")}1' OFS=, file
"ABC","0.20","2001-01-01"
"LMN","0.00","2008-01-01"
"LTD","0.20","2001-01-01"

测试是否3以 开头的字段BMR,如果是则将其设置为空白。
如果需要,Sub 删除额外的空字段。

于 2013-11-15T09:22:08.227 回答
0
sed 's/^\(\("[^"]*",\)\{2\}\)"BMR[^"]*",/\1/' YourFile

只要确保结构"blaba","blabla","BMR不包括之间的空间","

于 2013-11-15T09:27:34.947 回答