简体   繁体   中英

Match pattern and append at the end of line using sed

I have a bunch of lines for example:

hello.c                                 
Untitled Folder          
Shell.sh          
Linux        

and so on

what I want is to append / at the end of lines which do not contain . using sed. ie

hello.c                                                                                                                   
Untitled Folder/     
Shell.sh     
Linux/  

Can anybody help me ?

You can use an address:

sed -e '/\./!s=$=/='

Which means: if the line does not ( ! ) contain a dot ( /\\./ ), append /.

Something like that?

files = ['hello.c', 'Untitled Folder', 'Shell.sh', 'Linux']
for i, f in enumerate(files):
    if '.' not in f:
        files[i] = "%s/" % f

print files

ouput:

['hello.c', 'Untitled Folder/', 'Shell.sh', 'Linux/']

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