AWK / SED删除包含括号的列

时间:2010-10-16 16:38:26

标签: bash unix scripting sed awk

我想知道是否可以删除数据文件中包含任何可以包含在任何列中的括号的列。例如

...
John Doe (Tech Department) 09/12/2009 555-555-5555
Jane Smith 09/12/2009 555-555-5555 (Suspended)
Alfred doe 555-555-5555 (Vacation) 09/09/2011
...

那么我希望输出看起来像

...
John Do 09/12/2009 555-555-5555
Jane Smith 09/12/2009 555-555-5555
Alfred doe 555-555-5555 09/09/2011
...

我在考虑在括号之间使用通配符?类似/(*)/?

感谢您的帮助,

托梅克

3 个答案:

答案 0 :(得分:2)

$ cat file
John Doe (Tech Department) 09/12/2009 (somethign else) 555-555-5555
Jane Smith 09/12/2009 555-555-5555 (Suspended)
Alfred doe 555-555-5555 (Vacation) 09/09/2011

$ awk -vRS=")" -F"(" '{ for(i=1;i<=NF;i+=2) {print $i}}' ORS="" file
John Doe  09/12/2009  555-555-5555
Jane Smith 09/12/2009 555-555-5555
Alfred doe 555-555-5555  09/09/2011

$ sed -r 's/\(.[^)]*\)//g' file
John Doe  09/12/2009  555-555-5555
Jane Smith 09/12/2009 555-555-5555
Alfred doe 555-555-5555  09/09/2011

$ awk '{gsub(/\(.[^)]*\)/,"") }1' file
John Doe  09/12/2009  555-555-5555
Jane Smith 09/12/2009 555-555-5555
Alfred doe 555-555-5555  09/09/2011

答案 1 :(得分:1)

如果Perl没问题,你可以这样做:

$ cat file
John Doe (Tech Department) 09/12/2009 555-555-5555
Jane Smith 09/12/2009 555-555-5555 (Suspended)
Alfred doe 555-555-5555 (Vacation) 09/09/2011

$ perl -pe 's/\(.*?\)//g' file    
John Doe  09/12/2009 555-555-5555
Jane Smith 09/12/2009 555-555-5555 
Alfred doe 555-555-5555  09/09/2011

答案 2 :(得分:0)

最简单的是一行sed命令:

$ sed -e '/[()]/d' file    
John Doe  09/12/2009  555-555-5555
Jane Smith 09/12/2009 555-555-5555
Alfred doe 555-555-5555  09/09/2011