简体   繁体   中英

Is there a better way?

I have file say abc.txt with below format:

+ : @group2 : ALL  
+ : @grp_xvz : ALL  
+ : @group_abc_app: ALL  
+ : @group_1_abc : ALL  
+ : @group_2_xyz : ALL  
+ : @group_3_def@@nmo_hosts : ALL

I need to grep for specific entries and check if file size of abc.txt > 220

+ : @group_2_xyz : ALL   or 
+ : @group_3_def@@nmo_hosts : ALL   
and  filesize of abc.txt > 220  

In bash I can do like this

if grep --quiet "+[[:blank:]]:[[:blank:]]@group_2_xyz[[:blank:]]*:[[:blank:]]ALL" abc.txt   
||  
 grep --quiet +[[:blank:]]:[[:blank:]]@group_3_def[@A-Za-z0-9_][[:blank:]]:[[:blank:]] abc.txt   
and                                                   
[ du -sb abc.txt | awk '{print $1}' -gt 220 ]; then  

..do..something

How to do same in python? I was trying to use "re.findall' but not sure if I can use multiple conditions there ? or if someone can suggest best way?

re.findall(r'+\s*:\s*@group_2_xyz\s*:\s*ALL', open('abc.txt,'r').read())

Thanks in advance.

Try this:

import os, re

match = re.search(
    r'^\+ *: *(@group_2_xyz|@group_3_def@@nmo_hosts) *: *ALL$',
    open('abc.txt').read(), re.M
    )

print(os.stat('abc.txt').st_size > 220, match is not None)

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