当前位置: 首页 > 面试题库 >

如何将sed之类的文本替换为python?

扶绍辉
2023-03-14
问题内容

我想启用此文件中的所有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

使用sed,这是一种简单sed -i 's/^# deb/deb/' /etc/apt/sources.list的方法,最优雅(“
pythonic”)的方法是什么?


问题答案:

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

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

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

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

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

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

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


 类似资料:
  • 问题内容: 我有一个文件“ test.txt”,其中包含以下内容 我要做的是将它们替换为: 但是为什么这段代码不起作用? 什么是正确的方法? 问题答案: 使用代替。并添加标志以替换所有内容。

  • 我得到了一个导入的文本块,但格式并不总是那么完美。之后我会尝试用jquery解决这个问题。所以我开始用 替换 : null null 但替换不起作用。最终的html应该如下所示:

  • 我有一个文本字段,用户在其中写东西,我用javascript获取文本框的文本,并在段落中显示。我有文字限制的段落像50 chrac。如果用户编写的chrac超过50个,我想显示(…)45小时后,chrac。可以使用javascript吗? 像这样的段落是用户写的,它的50多个chrac。 Lorem ipsum dolor坐在那里,奉献给我们,我们的工作和伟大的礼物。在有限的条件下,在有限的条件下

  • 问题内容: 我一直在网上寻找这个答案,但似乎找不到。 我有一个配置文件,其中包含: 我想将VAR5的值从VALUE5更改为VALUE10。不幸的是,我不知道VALUE5的值,因此无法搜索。因此,基本上我需要使用sed(或其他任何值)将VAR5的值替换为我想替换为的任何值。 问题答案: 您可以尝试使用此sed: 它给:

  • 问题内容: 我有一个大文本文件(URL.txt),我希望使用一个 sed 命令执行以下操作: 查找行号19和33之间的文本“ google”并将其替换为“ facebook”。 在终端上显示输出而不更改原始文件。 问题答案: 您可以使用SED的范围选择器: 这将在19(不含)和33(含)之间的行上进行替换 请注意,这只会替换每行的第一次出现,您可以使用-modifier更改此行为:

  • 我很难匹配和捕捉这些。目前我正在尝试以下变体: 我认为sed有一些regex或捕获组行为我不理解