这是文本文件Drives.txt
:
Slot Number : 0
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 1
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 2
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
我想在每第12行(即第12行、第24行、第36行等)之后添加一个空行,这样每个已诊断的驱动器之间就会有一个空格。我该怎么做?我想在 bash 脚本中实现它。
输出必须如下所示:
Slot Number : 0
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 1
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 2
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
答案1
这是一个自然的适合awk
awk '{print} NR % 12 == 0 {print ""}' Drives.txt
答案2
类型
sed '12~12a\
' Drives.txt
>
(是的;这是两行单独的行;输入第一行后shell 会发出提示),或者
sed -e '12~12a\' -e '' Drives.txt
寻址模式是 GNU sed 的扩展,意思是“在线执行以下命令 M~N
M
以及N
此后的每一行”。 sed 中的命令 a
意思是“追加一行”。通常您会提供要插入的文本;因为你想要空行,所以我用了 ''
.
正如 Steeldriver 指出的那样,您也可以使用0~12
.由于没有第 0 行,因此它实际上从第 12 行开始。
答案3
对于任何实现,您都可以通过附加(空)保留空间来在行sed
后添加换行符:alert :
G
sed '/alert *:/G' Drives.txt
如果您不喜欢文件末尾的空行,则对于最后一行(地址$
)P
,仅打印换行符之前的部分,然后d
删除该行:
sed '/alert *:/G;$P;$d' Drives.txt
答案4
awk 'NR%12 == 0 {$0=$0"\n"}1' file.txt
输出
Slot Number : 0
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 1
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No
Slot Number : 2
Drive's position : DiskGroup 0
Media Error Count : 0
Other Error Count : 0
Predictive Failure Count : 0
Raw Size : 745.211 GB [0x5d26ceb0 Sectors]
Firmware state : Online, Spun Up
Inquiry Data : WDC WUSTR6480ASS200 K95DV6X3NG2A
Foreign State : None
Media Type : Solid State Device
Drive Temperature : 27C (80.60 F)
Drive S.M.A.R.T alert : No