我想从NetworkActivity_5851_*_09-04-2016.done
获取NetworkActivity_5851_2326316_09-04-2016.log.gz
字符串,这里是我写的代码
local file="$1"
local extension="${file##*.}"
if [ $extension = 'done' ]; then
local files=`basename $file`
files="${files#*_}"
files="${files#*_}"
files="${files%_*}"
local q=_"$files"_
local mask="${file/done/log.gz}"
mask="${mask/${q}/_*_}"
r=`ls "${mask}" | wc -l`
并且它工作正常,但是当我用python脚本运行它时它失败了。我的意思是r
变量有错误的值。
这是Python中的代码
shell = Shell(RUN_SCRIPT_2, LOGFILE)
Shell是
class Shell():
"""
Base class for the shell script object which
is under testing.
"""
def __init__(self, path_to_script, path_to_log=None):
"""
executes shell script and store results
of STDOUT and STDERR into appropriate attributes
"""
self.path_to_log = path_to_log
# clear log file before run
if self.path_to_log:
open(self.path_to_log, 'w').close()
shell = subprocess.Popen([path_to_script],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
# the line below makes sure shell command execution finished
self.stdout, self.stderr = shell.communicate()
self.log_data = self.get_log_data()
和文件path_to_script是
echo 'Start'
file="${SOURCE_DIR}/NetworkActivity_5851_3_09-04-2016.done"
extension="${file##*.}"
if [ $extension = 'done' ]; then
files=`basename $file`
files="${files#*_}"
files="${files#*_}"
files="${files%_*}"
q=_"$files"_
mask="${file/done/log.gz}"
mask="${mask/${q}/_*_}"
r=`ls "${mask}" | wc -l`
echo $r
if [ $r = $files ]; then
rez=0
else rez=1
fi
fi
if [[ $rez -eq 1 ]]; then
echo "Failure"
else echo "Success"
fi
echo 'Finish'
因此,当我从终端r
运行path_to_script时,变量设置文件数量,当我使用Python运行它时,它设置为0.
答案 0 :(得分:0)
你可以单独使用bash
字符串操作技术来做到这一点。
$ inputString="NetworkActivity_5851_2326316_09-04-2016.log.gz"
$ substring="${inputString%%.*}" # Removing the part after the first '.'
$ [[ $substring =~ .*_([[:digit:]]+)_.* ]] && NUM=${BASH_REMATCH[1]} # Extracting the number you want to replace
$ finalString="${substring/$NUM/*}.done" # Forming the final string with the extension
$ printf "%s\n" "$finalString"
NetworkActivity_5851_*_09-04-2016.done
您可以将此逻辑放在一个shell脚本中,并为您拥有的多个文件运行相同的逻辑。上述命令可以直接在控制台上运行。
答案 1 :(得分:0)
好吧,我基于你的第一段。希望我遇到了问题!这是我使用Perl和带有简单命令行的正则表达式的解决方案。
~$ gunzip NetworkActivity_5851_2326316_09-04-2016.log.gz && perl -e 'while(<>){print $_ if $_ =~ /NetworkActivity_\d+_.*_\d{2}\-\d{2}-\d{4}\.done/}' NetworkActivity_5851_2326316_09-04-2016.log
希望它有所帮助!