AWK/SED 删除包含括号的列

发布于 2024-09-27 22:23:35 字数 446 浏览 3 评论 0原文

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

...
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
...

我正在考虑对括号之间的文本使用通配符?像/(*)/之类的东西?

感谢您的帮助,

托梅克

I was wondering if it would be possible to remove columns within a data file that contain any parenthesis that can be contained in ANY column. For example

...
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
...

So then I would like the output to look like

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

I was thinking of using a wildcard for the text in between the parenthesis? something like /(*)/ ?

Thanks for any help,

Tomek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走干脆点 2024-10-04 22:23:35
$ 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
$ 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
假面具 2024-10-04 22:23:35

如果 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

If Perl is okay, you can do:

$ 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
野鹿林 2024-10-04 22:23:35

最简单的是一行 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

The simplest is a one-line sed command:

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文