简体   繁体   中英

How do I filter out lines of a text file that have length of 8 and ends in .com?

I have a list of a million domain names in name.txt

hello.com
abc.com
gogogo.us
goodbye.me
...
...

How do I pipe only domain names with 8 letters (including the .com ) and only ends in .com to names_new.txt ?

I'm looking for a simple command and not a script or anything.

grep是第一个用于模式匹配的工具:

egrep -x '[a-z]{4}\.com' name.txt > newname.txt

尝试

 egrep "^[a-z][a-z][a-z][a-z]\.com$" name.txt > names_new.txt

Use Awk. The domain name is split by . into fields.

First field is tested for length 4,as the .com adds another 4 chars.

The second field should contain com .

When both conditions are met, the line is printed.

cat name.txt |awk -F. '((length($1)==4)&&($2=="com")){print;}' > names_new.txt 

Note: the line may found false positives if you have subdomains, eg: mail.com.nz

There may be domain names with dashes or numbers.
-i forces egrep to match regardless of case.

egrep -i "^[a-z0-9-]{4}\.com$" name.txt > names_new.txt

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