简体   繁体   English

使用 Sed/Awk 仅删除 linux 文件中的字母字符行

[英]Remove only Alphabetic Character lines in a linux file by using Sed/Awk

I have a linux file.我有一个 linux 文件。 In that file, some lines are there merge as with numeric and alpha charcters.在该文件中,某些行与数字和字母字符合并。 I want to remove only alpha characters lines..我只想删除字母字符行..

$ cat file  
 ###John Daggett King Road, Plymouth MA##

Alice Ford, 22 East Broadway, Richmond VA

Orville Thomas, 11345 Oak Bridge Road, Tulsa OK

 #Terry Kalkas  Lans Road, Beaver Falls PA

Eric Adams, 20 Post Road, Sudbury MA
##Hubert Sims,A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

Let us consider a file with the sample contents as below:让我们考虑一个包含以下示例内容的文件:

remove only #Terry Kalkas Lans Road, Beaver Falls PA kind of lines.仅删除#Terry Kalkas Lans Road, Beaver Falls PA类的线条。 I want to see我想看看

Amy Wilde, 334 Bayshore Pkwy, Mountain View CA

Sal Carpenter, 73 6th Street, Boston MA

Please share your Ideas请分享您的想法

You can remove all lines that have no digit with this sed :您可以使用此sed删除所有没有数字的行:

sed '/[0-9]/!d' file
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Eric Adams, 20 Post Road, Sudbury MA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA
grep '[0-9]' YourFile

will catch only line with at least 1 digit inside (removing other) like you ask将只捕获内部至少有 1 位数字的行(删除其他),就像你问的那样

It also remove empty line because there is no digit inside它还删除行,因为里面没有数字

grep -E -e '[0-9]|(^[[:space:]]*$)' YourFile

keep both empty line and line with digit inside保留空行和里面有数字的行

It seems that yout input-file uses # as a comment-character (everything that comes after the comment is ignored).似乎您的输入文件使用#作为注释字符(注释之后的所有内容都被忽略)。

If your actual intention is to only display the lines that are active and discard the ones that are commented out or empty (though this is not what you asked for), then do something like:如果您的实际意图是只显示用的线,并丢弃被注释掉的那些或空(虽然这不是你问什么),然后像做:

sed -e 's/#.*//' -e '/^[[:space:]]*$/d'

The first expression ( s|#.*|| ) will remove all content after the comment-character, whereas the second expression ( /^[[:space:]]*$/d ) will delete all empty lines (and lines consisting only of whitespace).第一个表达式 ( s|#.*|| ) 将删除注释字符之后的所有内容,而第二个表达式 ( /^[[:space:]]*$/d ) 将删除所有空行(以及包含仅空格)。

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

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