怎么做sed像文本替换python?

Max*_*ler 46 python regex linux

我想在此文件中启用所有apt存储库

cat /etc/apt/sources.list
## Note, this file is written by cloud-init on first boot of an instance                                                                                                            
## modifications made here will not survive a re-bundle.                                                                                                                            
## if you wish to make changes you can:                                                                                                                                             
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg                                                                                                                
##     or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d                                                                                                                                       
#                                                                                                                                                                                   

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to                                                                                                           
# newer versions of the distribution.                                                                                                                                               
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick main                                                                                                                   
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick main                                                                                                               

## Major bug fix updates produced after the final release of the                                                                                                                    
## distribution.                                                                                                                                                                    
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates main                                                                                                           
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates main                                                                                                       

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu                                                                                                         
## team. Also, please note that software in universe WILL NOT receive any                                                                                                           
## review or updates from the Ubuntu security team.                                                                                                                                 
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick universe                                                                                                               
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick universe                                                                                                           
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick multiverse
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates multiverse

## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu maverick partner
# deb-src http://archive.canonical.com/ubuntu maverick partner

deb http://security.ubuntu.com/ubuntu maverick-security main
deb-src http://security.ubuntu.com/ubuntu maverick-security main
deb http://security.ubuntu.com/ubuntu maverick-security universe
deb-src http://security.ubuntu.com/ubuntu maverick-security universe
# deb http://security.ubuntu.com/ubuntu maverick-security multiverse
# deb-src http://security.ubuntu.com/ubuntu maverick-security multiverse
Run Code Online (Sandbox Code Playgroud)

使用sed这是一个简单sed -i 's/^# deb/deb/' /etc/apt/sources.list的最优雅("pythonic")方式吗?

Dav*_*ler 51

你可以这样做:

with open("/etc/apt/sources.list", "r") as sources:
    lines = sources.readlines()
with open("/etc/apt/sources.list", "w") as sources:
    for line in lines:
        sources.write(re.sub(r'^# deb', 'deb', line))
Run Code Online (Sandbox Code Playgroud)

with语句确保文件正确关闭,并且"w"在写入文件之前重新打开文件会清空文件.re.sub(pattern,replace,string)相当于sed/perl中的s/pattern/replace /.

编辑:示例中的固定语法

  • 正如 plundra 所指出的,您的解决方案以非原子方式写入,因此会引发竞争条件(例如,其他进程和/或线程在重写此类文件时尝试同时读取此类文件)。这是个问题。但它仍然优雅而活泼。 (3认同)

Cec*_*rry 27

sed没有外部命令或额外依赖性的情况下,用纯Python 编写本土替代品是一项充满高贵地雷的崇高任务.谁曾想到?

尽管如此,这是可行的.这也是可取的.我们一直都在那里,人们:"我需要一些明文文件,但我只有Python,两个塑料鞋带,以及发霉的罐装级Maraschino樱桃.帮助."

在这个答案中,我们提供了一个最好的解决方案,将先前答案的精彩程度拼凑在一起,而没有那些令人不快的令人愉快的事情.正如plundra所说,David Miller的其他顶级答案非原子地写入所需文件,因此会引发竞争条件(例如,来自尝试同时读取该文件的其他线程和/或进程).那很糟.Plundra的优秀答案解决了这个问题,同时引入了更多 - 包括众多致命的编码错误,一个关键的安全漏洞(未能保留原始文件的权限和其他元数据),以及用低级别字符索引替换正则表达式的过早优化.这也很糟糕.

太棒了,团结一致!

import re, shutil, tempfile

def sed_inplace(filename, pattern, repl):
    '''
    Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
    `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
    '''
    # For efficiency, precompile the passed regular expression.
    pattern_compiled = re.compile(pattern)

    # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
    # writing with updating). This is usually a good thing. In this case,
    # however, binary writing imposes non-trivial encoding constraints trivially
    # resolved by switching to text writing. Let's do that.
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
        with open(filename) as src_file:
            for line in src_file:
                tmp_file.write(pattern_compiled.sub(repl, line))

    # Overwrite the original file with the munged temporary file in a
    # manner preserving file attributes (e.g., permissions).
    shutil.copystat(filename, tmp_file.name)
    shutil.move(tmp_file.name, filename)

# Do it for Johnny.
sed_inplace('/etc/apt/sources.list', r'^\# deb', 'deb')
Run Code Online (Sandbox Code Playgroud)

  • @Adrian 这本身并不是一个失败 - 因为它失败只是因为 Python 与 Sed 使用的正则表达式方言不同,它不会将 & 解释为模式的内容 - 在这种情况下你应该使用 \0。简单地告诉用户他们应该使用 Python 的 re 方言就可以了。但在这种情况下,名称中可能不应该包含“sed”。 (5认同)
  • 在以下情况下会失败 `s/^Q(.*)/"&"/`:您将用文字 `"&"` 替换匹配项,而不是用引号将匹配项括起来的预期任务 (2认同)

elm*_*tec 21

massedit.py(http://github.com/elmotec/massedit)为你做脚手架只留下正则表达式写.它仍处于测试阶段,但我们正在寻求反馈.

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" /etc/apt/sources.list
Run Code Online (Sandbox Code Playgroud)

将以diff格式显示差异(之前/之后).

添加-w选项以将更改写入原始文件:

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" -w /etc/apt/sources.list
Run Code Online (Sandbox Code Playgroud)

或者,您现在可以使用api:

>>> import massedit
>>> filenames = ['/etc/apt/sources.list']
>>> massedit.edit_files(filenames, ["re.sub(r'^# deb', 'deb', line)"], dry_run=True)
Run Code Online (Sandbox Code Playgroud)


plu*_*dra 12

这是一种不同的方法,我不想编辑我的其他答案.嵌套,with因为我不使用3.1(Where with A() as a, B() as b:works).

可能是有点大材小用改变的sources.list,但我希望把它在那里为将来的搜索.

#!/usr/bin/env python
from shutil   import move
from tempfile import NamedTemporaryFile

with NamedTemporaryFile(delete=False) as tmp_sources:
    with open("sources.list") as sources_file:
        for line in sources_file:
            if line.startswith("# deb"):
                tmp_sources.write(line[2:])
            else:
                tmp_sources.write(line)

move(tmp_sources.name, sources_file.name)
Run Code Online (Sandbox Code Playgroud)

这应该确保其他人阅读文件没有竞争条件.哦,如果没有正则表达式,我更喜欢str.startswith(...).

  • @Mark Longair:`os.rename`在文件系统之间不起作用.例如,如果`/ tmp`在`tmpfs`上,它就会失败. (2认同)

M. *_*del 6

If you are using Python3 the following module will help you: https://github.com/mahmoudadel2/pysed

wget https://raw.githubusercontent.com/mahmoudadel2/pysed/master/pysed.py
Run Code Online (Sandbox Code Playgroud)

Place the module file into your Python3 modules path, then:

import pysed
pysed.replace(<Old string>, <Replacement String>, <Text File>)
pysed.rmlinematch(<Unwanted string>, <Text File>)
pysed.rmlinenumber(<Unwanted Line Number>, <Text File>)
Run Code Online (Sandbox Code Playgroud)


Mat*_*ice 6

如果我想要sed 这样的东西,那么我通常只是sed使用sh库来调用它自己。

from sh import sed

sed(['-i', 's/^# deb/deb/', '/etc/apt/sources.list'])
Run Code Online (Sandbox Code Playgroud)

当然,也有缺点。就像本地安装的版本sed与您测试的版本不同一样。在我的例子中,这种事情可以在另一层轻松处理​​(例如通过事先检查目标环境,或者使用已知版本的 sed 部署在 docker 映像中)。


小智 5

尝试pysed

pysed -r '# deb' 'deb' /etc/apt/sources.list
Run Code Online (Sandbox Code Playgroud)