在中查找理解sed命令的帮助操作系统Python线()

2024-05-15 02:10:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在把我写的一些代码翻译成一个并行进程,然后分发到我家大学的计算机集群上。为了准备为集群编写脚本,我首先阅读了集群提供的Python代码示例:

#! /usr/bin/python

# This script replicates a given test with varied parameters
# creating unique submit scripts and executing the submission to the CRC SGE queue

# Imports
import os
import shutil
import string

basedir=os.getcwd()
origTestFol='wwd'
templFol='template'
origTestDir= basedir + '/' + origTestFol
templFolDir= basedir + '/' + templFol

steps=[0,1,2,3,4,5,6,7,8,9]
primes=[2,3,5,7,11,13,17,19,23,29,31]
trials=[6,7,8,9,10]

for step in steps:
    newTestDir= origTestDir + '_nm_' + str(step)
    if not os.path.exists(newTestDir):
        os.mkdir(newTestDir)
    os.chdir(newTestDir)
    for trial in trials:
       newTestSubDir= newTestDir + '/' + str(trial)
       if not os.path.exists(newTestSubDir):
            shutil.copytree(templFolDir,newTestSubDir)
            os.chdir(newTestSubDir)   
            os.system('sed -i \'s/seedvalue/' + str(primes[trial]) + '/g\' wwd.nm.conf')
            os.system('sed -i \'s/stepval/' + str(step) + '/g\' qsubScript.sh')
            os.system('qsub qsubScript.sh')
            os.chdir(basedir)

我可以跟踪代码到最后四行[例如,直到”操作系统('sed-i…“)但是很难遵循代码。有没有其他人可以帮我理解这最后四行的意思。有没有一种方法来描述伪代码中的谎言?据我所知,第一行sed试图用素数的值替换“seedvalue”,但我不确定seedvalue是什么。我也不知道怎么在后面的那一行。如果其他人能对这些问题有所了解,我们将不胜感激。在


Tags: the代码importosstep集群systemsed
2条回答

你可以在sed上搜索一下:http://en.wikipedia.org/wiki/Sed#In-place_editing

简而言之:sed -i 's/old/new/g' filefile中的old替换所有出现的new-i标志告诉它以内联方式执行,修改文件本身。在

在您的代码中,seedvalue和{}只是文本文件wwd.nm.confqsubScript.sh中的两个单词。这些命令正在替换这些单词,就像在文本编辑器或字处理程序中那样。在

seedvalue是一个字符串,因此它的值是它本身,与stepval相同。在

第一行sed将用值str(primes[trial])替换wwd.nm.conf中的所有seedvalue(在所有行上,/g就是这样,它代表“global”)。这样想:

无论文本stepvalue在文件wwd.nm.conf中的任何位置,都要输入值str(primes[trial])(在Python中计算结果是什么)。在

第二个sed调用将执行类似的操作,但是使用stepval和{}的结果,它将替换文件qsubScript.sh中的文本。在

相关问题 更多 >

    热门问题