简体   繁体   中英

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

I have a linux file. 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. 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 '/[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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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