我的脚本格式如下。
在我的示例中,我需要搜索“ Probable Cause:Unspecified
”,一旦发现,则需要用下面的第四行替换行。
实际输出为:
Serial : blah blah blah
A-TYPE: blah blah blah
O-type: blah blah blah
instance: blah blah blah
fault: blah blah blah
sev: blah blah blah
Probable Cause: Unspecified reason
ack: blah blah blah
time: blah blah blah
Specific Event MOC: blah blah blah
cause: blah blah blah
Specific Problem: blah blah blah
,然后将找到的字符串“可能的原因:未指明的原因” Probable Cause
替换为Specific Event MOC
需要最终输出:
Serial : blah blah blah
A-TYPE: blah blah blah
O-type: blah blah blah
instance: blah blah blah
fault: blah blah blah
sev: blah blah blah
Specific Event MOC: Unspecified reason
ack: blah blah blah
time: blah blah blah
Probable Cause: blah blah blah
cause: blah blah blah
Specific Problem: blah blah blah
请帮助
答案 0 :(得分:1)
这是2次通过awk
解决方案:
awk 'BEGIN{FS=OFS=":"}
FNR == NR {
r[FNR]=$1
next
}
/^Probable Cause:/ {
$1 = r[FNR+3]
n=FNR
}
FNR == n+3 && /^Specific Event / {
$1 = r[FNR-3]
} 1' file file
Serial : blah blah blah
A-TYPE: blah blah blah
O-type: blah blah blah
instance: blah blah blah
fault: blah blah blah
sev: blah blah blah
Specific Event MOC: Unspecified reason
ack: blah blah blah
time: blah blah blah
Probable Cause: blah blah blah
cause: blah blah blah
Specific Problem: blah blah blah
说明:
BEGIN
块将输入和输出字段分隔符设置为:
FNR == NR
:第一次处理文件时r[FNR]=$1
:按键存储第一列作为记录编号next
:移至下一条记录/^Probable Cause:/
:当我们在记录中找到此文本时$1 = r[FNR+3]
:将第一列设置为当前记录前面的第三条记录的第一列n=FNR
:将当前记录存储在变量n
FNR == n+3 && /^Specific Event /
:当前记录为n+3
时,我们以Specific Event
开头的行$1 = r[FNR-3]
:将第1列设置为当前当前第3条记录的第1列答案 1 :(得分:1)
您在'd'中的数据,由gnu sed;
from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import weakref
from kivy.clock import Clock
kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager:
transition: FadeTransition()
ShipmentsScreen:
<TextInput@TextInput>
size_hint_y: None
height: 20
<Row>:
cols: 3
TextInput:
TextInput:
TextInput:
<Rows>:
orientation: 'vertical'
<ShipmentsScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Store'
size_hint_y: None
height: 20
on_press: rows.store()
Rows:
id: rows
"""
class Row(GridLayout):
pass
class Rows(BoxLayout):
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
Clock.schedule_once(self.fill)
self._rows = {}
def fill(self, dt):
for i in range(30):
row = Row()
self.add_widget(row)
self._rows["row"+str(i)] = weakref.ref(row)
def store(self):
#loop that will access each textinput and print its content
print("textinput content by id or coordinates x, y")
class ShipmentsScreen(Screen):
pass
sm = Builder.load_string(kv)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()