我试图替换像这种模式的行块:
让我们看一个例子,这个输入:
01 hello
02 stack
02 overflow
04 hi
02 friends = overflow
03 this
03 is
03 my = is
03 life
02 lol
02 im
02 joking = im
03 filler
将生成以下输出(每个hello块是数组的一个元素):
01 hello
02 stack
02 overflow
04 hi
02 lol
02 im
01 hello
02 stack
02 overflow
04 hi
02 lol
02 joking = im
03 filler
01 hello
02 stack
02 friends = overflow
03 this
03 is
03 life
02 lol
02 im
01 hello
02 stack
02 friends = overflow
03 this
03 is
03 life
02 lol
02 joking = im
03 filler
01 hello
02 stack
02 friends = overflow
03 this
03 my = is
03 life
02 lol
02 im
01 hello
02 stack
02 friends = overflow
03 this
03 my = is
03 life
02 lol
02 joking = im
03 filler
我通过这种方式尝试过:
#!/bin/bash
awk '{
if ($0~/=/){
level=$1
oc=1
}else if (oc && $1<=level){
oc=0
}
if (!oc){
print
}
}' input.txt
但它只返回我需要的第一个输出,而且我不知道如何跳过03 life
内的friends
字。
我怎样才能生成这些输出?
如果对你更加轻松,我不会介意python或perl解决方案。
答案 0 :(得分:7)
这是一个python脚本,用于读取cobol输入文件并打印出已定义和重新定义的变量的所有可能组合:
#!/usr/bin/python
"""Read cobol file and print all possible redefines."""
import sys
from itertools import product
def readfile(fname):
"""Read cobol file & return a master list of lines and namecount of redefined lines."""
master = []
namecount = {}
with open(fname) as f:
for line in f:
line = line.rstrip(' .\t\n')
if not line:
continue
words = line.split()
n = int(words[0])
if '=' in words or 'REDEFINES' in words:
name = words[3]
else:
name = words[1]
master.append((n, name, line))
namecount[name] = namecount.get(name, 0) + 1
# py2.7: namecount = {key: val for key, val in namecount.items() if val > 1}
namecount = dict((key, val) for key, val in namecount.items() if val > 1)
return master, namecount
def compute(master, skip=None):
"""Return new cobol file given master and skip parameters."""
if skip is None:
skip = {}
seen = {}
skip_to = None
output = ''
for n, name, line in master:
if skip_to and n > skip_to:
continue
seen[name] = seen.get(name, 0) + 1
if seen[name] != skip.get(name, 1):
skip_to = n
continue
skip_to = None
output += line + '\n'
return output
def find_all(master, namecount):
"""Return list of all possible output files given master and namecount."""
keys = namecount.keys()
values = [namecount[k] for k in keys]
out = []
for combo in product(*[range(1, v + 1) for v in values]):
skip = dict(zip(keys, combo))
new = compute(master, skip=skip)
if new not in out:
out.append(new)
return out
def main(argv):
"""Process command line arguments and print results."""
fname = argv[-1]
master, namecount = readfile(fname)
out = find_all(master, namecount)
print('\n'.join(out))
if __name__ == '__main__':
main(sys.argv)
如果将上述脚本保存在名为cobol.py
的文件中,则可以如下运行:
python cobol.py name_of_input_file
定义和重新定义的各种可能组合将显示在stdout上。
此脚本在python2(2.6+)或python3下运行。
代码使用三个函数:
readfile
读取输入文件并返回两个变量,用于汇总其中的内容结构。
compute
有两个参数,从中计算输出块。
find_all
确定所有可能的输出块,使用compute
创建它们,然后将它们作为列表返回。
让我们更详细地看一下每个函数:
readfile
readfile
将输入文件名作为参数,并返回一个列表master
和一个字典namecount
。对于输入文件中的每个非空行,列表master
具有包含(1)级别编号的元组,(2)定义或重新定义的名称,以及(2)原始行本身。对于示例输入文件,readfile
会返回master
的此值:
[(1, 'hello', '01 hello'),
(2, 'stack', ' 02 stack'),
(2, 'overflow', ' 02 overflow'),
(4, 'hi', ' 04 hi'),
(2, 'overflow', ' 02 friends = overflow'),
(3, 'this', ' 03 this'),
(3, 'is', ' 03 is'),
(3, 'is', ' 03 my = is'),
(3, 'life', ' 03 life'),
(2, 'lol', ' 02 lol'),
(2, 'im', ' 02 im'),
(2, 'im', ' 02 joking = im'),
(3, 'filler', ' 03 filler')]
readfile
还返回字典namecount
,该字典具有重新定义的每个名称的条目,并且具有该名称的定义/重新定义的数量。对于示例输入文件,namecount
具有值:
{'im': 2, 'is': 2, 'overflow': 2}
这表明im
,is
和overflow
各有两个可能的值。
readfile
当然是设计用于在当前版本的问题中使用输入文件格式。在可能的范围内,它也被设计为使用此问题的先前版本的格式。例如,无论是使用等号(当前版本)还是使用与先前版本中的单词REFDEFINES
一起发信号,都可以接受变量重新定义。这是为了使这个脚本尽可能灵活。
compute
函数compute
是生成每个输出块的函数。它使用两个参数。第一个是master
,直接来自readfile
。第二个是skip
,它派生自namecount
返回的readfile
字典。例如,namecount
字典表示im
有两种可能的定义。这显示了compute
如何用于为每个生成输出块:
In [14]: print compute(master, skip={'im':1, 'is':1, 'overflow':1})
01 hello
02 stack
02 overflow
04 hi
02 lol
02 im
In [15]: print compute(master, skip={'im':2, 'is':1, 'overflow':1})
01 hello
02 stack
02 overflow
04 hi
02 lol
02 joking = im
03 filler
观察到上面对compute
的第一次调用生成了使用im
的第一个定义的块,第二个调用生成了使用第二个定义的块。
find_all
有了上述两个功能,很明显最后一步只是生成所有不同的定义组合并将其打印出来。这就是函数find_all
的作用。使用master
返回的namecount
和readfile
,系统会运行所有可用的定义组合并调用compute
来为每个组合创建一个块。它收集所有可以通过这种方式创建的独特块并返回它们。
find_all
返回的输出是一个字符串列表。每个字符串都是对应于define / redefines的一个组合的块。使用问题中的示例输入,显示find_all
返回的内容:
In [16]: find_all(master, namecount)
Out[16]:
['01 hello\n 02 stack\n 02 overflow\n 04 hi\n 02 lol\n 02 im\n',
'01 hello\n 02 stack\n 02 friends = overflow\n 03 this\n 03 is\n 03 life\n 02 lol\n 02 im\n',
'01 hello\n 02 stack\n 02 overflow\n 04 hi\n 02 lol\n 02 joking = im\n 03 filler\n',
'01 hello\n 02 stack\n 02 friends = overflow\n 03 this\n 03 is\n 03 life\n 02 lol\n 02 joking = im\n 03 filler\n',
'01 hello\n 02 stack\n 02 friends = overflow\n 03 this\n 03 my = is\n 03 life\n 02 lol\n 02 im\n',
'01 hello\n 02 stack\n 02 friends = overflow\n 03 this\n 03 my = is\n 03 life\n 02 lol\n 02 joking = im\n 03 filler\n']
例如,让我们选择find_all
返回的第四个字符串,为了更好的格式,我们将print
:
In [18]: print find_all(master, namecount)[3]
01 hello
02 stack
02 friends = overflow
03 this
03 is
03 life
02 lol
02 joking = im
03 filler
在完整的脚本中,find_all
的输出组合在一起并打印到stdout,如下所示:
out = find_all(master, namecount)
print('\n'.join(out))
这样,输出显示所有可能的块。
awk 'f==0 && !/REDEFINES/{s=s"\n"$0;next} /REDEFINES/{f=1;print s t>("output" ++c ".txt");t=""} {t=t"\n"$0} END{print s t>("output" ++c ".txt")}' input
该程序具有以下变量:
f
是一个标志,在第一个REDEFINE之前为零,之后为一个。
s
包含第一个REDEFINE之前的所有文字。
t
包含当前REDEFINE的文字。
c
是一个计数器,用于确定输出名称的名称。
代码的工作原理如下:
f==0 && !/REDEFINES/{s=s"\n"$0;next}
在遇到第一次重新定义之前,文本保存在变量s
中,我们跳过其余命令并跳转到next
行。
/REDEFINES/{f=1;print s t>("output" ++c ".txt");t=""}
每当我们遇到REDEFINE行时,我们将标志f
设置为1,并将序言部分s
和当前的REDEFINE部分打印到名为outputn.txt
的文件中,其中n由计数器c
的值替换。
因为我们处于新的REDEFINE部分的开头,所以变量t
设置为空。
{t=t"\n"$0}
将此REDEFINE的当前行保存到变量t
。
END{print s t>("output" ++c ".txt")}
打印最后一个REDEFINE部分的输出文件。
上面代码生成的每个输出文件都有一个前导空白行。以下代码通过awk
substr
函数删除了该代码:
awk '/REDEFINES/{f=1;print substr(s,2) t>("output" ++c ".txt");t=""} f==0 {s=s"\n"$0;next} {t=t"\n"$0} END{print substr(s,2) t>("output" ++c ".txt")}' input
对于多样性,此版本的逻辑略有不同,否则会达到相同的结果。
awk 'f==1 && pre==$1 && !/REDEFINES/{tail=tail "\n" $0} /REDEFINES/{pre=$1;f=1;t[++c]="\n"$0} f==0 {head=head"\n"$0;next} pre!=$1{t[c]=t[c]"\n"$0} END{for (i=0;i<=c;i++) {print head t[i] tail>("output" (i+1) ".txt")}}' file