bash 如何从包含搜索词完全匹配的 CSV 文件中删除所有行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10208206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all lines from a CSV file containing an exact match of a search term?
提问by Village
I have a CSV file like this:
我有一个像这样的 CSV 文件:
text,0
more text,2
some more text,100
I need to delete any line containing only 0in the second column, e.g., the output of the above would be:
我需要删除仅包含0在第二列中的任何行,例如,上面的输出将是:
more text,2
some more text,100
How can I delete all lines from a CSV with an exact match?
如何从完全匹配的 CSV 文件中删除所有行?
回答by Karoly Horvath
If that's your last field, grep will do the trick:
如果这是您的最后一个字段,grep 会解决问题:
grep -v ',0$'
If not, and your fields don't contain ,, use awk:
如果没有,并且您的字段不包含,,请使用 awk:
awk -F , '{if (!='0') print}'
If it's even more complex use python or ruby with a CSV parser.
如果它更复杂,请使用带有 CSV 解析器的 python 或 ruby。
回答by Peter.O
A simple sed solution...
一个简单的 sed 解决方案...
sed /,0$/d
回答by E1Suave
If you want an inline edit to your file you could try the following. REMEMBER TO BACKUP YOUR FILE PRIOR TO ATTEMPTING ANY INLINE EDIT. (Will delete lines that contain a match)
如果您想对文件进行内联编辑,您可以尝试以下操作。请记住在尝试任何内联编辑之前备份您的文件。(将删除包含匹配项的行)
sed -i "s/.*match.*//g" /yourfile.csv
To delete any blank lines within your file. (REMEMBER TO BACKUP YOUR FILE PRIOR TO ATTEMPTING ANY INLINE EDIT.)
删除文件中的任何空行。(请记住在尝试任何内联编辑之前备份您的文件。)
sed -i "/^$/d" /yourfile.csv
For OS Xyou can try the following (REMEMBER TO BACKUP YOUR FILE PRIOR TO ATTEMPTING ANY INLINE EDIT.)
对于 OS X,您可以尝试以下操作(请记住在尝试任何内联编辑之前备份您的文件。)
sed -i '' "s/.*match.*//g" /yourfile.csv
sed -i '' "/^$/d" /yourfile.csv
回答by potong
This might work for you:
这可能对你有用:
awk -F, '' file

