grep -E 'READ:|WRITE
时间: 2023-11-09 11:07:34 浏览: 40
grep -E 'READ:|WRITE:'的作用是什么?
答案:grep -E 'READ:|WRITE:'的作用是在文件中查找包含“READ:”或“WRITE:”的行。
1. 如何使用grep命令查找多个文件中的匹配项?
2. grep命令中的-e和-E有什么区别?
3. 如何使用grep命令查找不包含某个字符串的行?
相关问题
python 脚本:长跑之前检查 linux下获取”cat /proc/cpuinfo | grep -i 'Model name' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}' dmidecode -t processor | grep -c Version dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}' dmidecode -t memory | grep -i size | grep -cvi no lspci | grep Eth | awk -F ': ' '{$1="";sub("^ *","");$2="";sub(" *$","");print}'“并保存结果,执行ipmitool power cycle,进入系统后继续检查”cat /proc/cpuinfo | grep -i 'Model name' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}' dmidecode -t processor | grep -c Version dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}' dmidecode -t memory | grep -i size | grep -cvi no lspci | grep Eth | awk -F ': ' '{$1="";sub("^ *","");$2="";sub(" *$","");print}' 脚本循环20次。
这是一个检查 Linux 系统硬件信息的 Python 脚本,执行过程如下:
1. 使用命令 `cat /proc/cpuinfo | grep -i 'Model name' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}'` 获取 CPU 型号信息;
2. 使用命令 `dmidecode -t processor | grep -c Version` 获取 CPU 版本信息;
3. 使用命令 `dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM' | awk -F: '{$1="";sub("^ *","");$2="";sub(" *$","");print}'` 获取内存型号信息;
4. 使用命令 `dmidecode -t memory | grep -i size | grep -cvi no` 获取内存大小信息;
5. 使用命令 `lspci | grep Eth | awk -F ': ' '{$1="";sub("^ *","");$2="";sub(" *$","");print}'` 获取网卡型号信息;
6. 将上述信息保存到文件中;
7. 执行 `ipmitool power cycle` 命令重启系统;
8. 进入系统后重复执行步骤 1-5;
9. 循环执行上述步骤 20 次。
下面是实现这个脚本的 Python 代码:
```python
import os
count = 20
for i in range(count):
# 获取 CPU 型号信息
cpu_model = os.popen("cat /proc/cpuinfo | grep -i 'Model name' | awk -F: '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
# 获取 CPU 版本信息
cpu_version = os.popen("dmidecode -t processor | grep -c Version").read().strip()
# 获取内存型号信息
memory_model = os.popen("dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM' | awk -F: '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
# 获取内存大小信息
memory_size = os.popen("dmidecode -t memory | grep -i size | grep -cvi no").read().strip()
# 获取网卡型号信息
nic_model = os.popen("lspci | grep Eth | awk -F ': ' '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
# 将信息保存到文件中
with open("hardware_info.txt", "a") as f:
f.write(f"CPU 型号: {cpu_model}\n")
f.write(f"CPU 版本: {cpu_version}\n")
f.write(f"内存型号: {memory_model}\n")
f.write(f"内存大小: {memory_size}\n")
f.write(f"网卡型号: {nic_model}\n")
f.write("\n")
# 重启系统
os.system("ipmitool power cycle")
# 等待系统重启完成
time.sleep(60)
# 再次获取硬件信息
cpu_model = os.popen("cat /proc/cpuinfo | grep -i 'Model name' | awk -F: '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
cpu_version = os.popen("dmidecode -t processor | grep -c Version").read().strip()
memory_model = os.popen("dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM' | awk -F: '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
memory_size = os.popen("dmidecode -t memory | grep -i size | grep -cvi no").read().strip()
nic_model = os.popen("lspci | grep Eth | awk -F ': ' '{$1=\"\";sub(\"^ *\",\"\",$2);$2=\"\";sub(\" *$\",\"\",$2);print}'").read().strip()
# 将信息保存到文件中
with open("hardware_info.txt", "a") as f:
f.write(f"重启后 CPU 型号: {cpu_model}\n")
f.write(f"重启后 CPU 版本: {cpu_version}\n")
f.write(f"重启后 内存型号: {memory_model}\n")
f.write(f"重启后 内存大小: {memory_size}\n")
f.write(f"重启后 网卡型号: {nic_model}\n")
f.write("\n")
```
注意,如果要执行 `ipmitool power cycle` 命令需要使用 root 权限。此外,为了防止重启过程中文件写入失败,最好将文件名改为绝对路径。
以下shell脚本是什么意思,请转成python脚本。HDDINFO_FILE="/root/hddinfo.txt" cp -rf /root/TestPlat/common/slot_sn.ini $INITIALFILE G_SCAN_PN=$(cat /root/TestPlat/common/slot_sn.ini | head -1 | awk -F "PN:" '{print $2}' | awk -F ";" '{print $1}') dos2unix accurate.txt dos2unix -f accurate.txt cat "accurate.txt" | grep "NVME-SLOT-" | awk -F "->" '{print $3}' | awk -F "(" '{print $1}' | tee poweron_slotbus.txt cat "accurate.txt" | grep "NVME-SLOT-" | awk -F "->" '{print $4}' | awk -F "(" '{print $1}' | tee poweron_diskbus.txt paste poweron_slotbus.txt poweron_diskbus.txt | head -n 24 | tail -n 23 | tee poweron_info.txt lsmod | grep -iw "nvme" if [ $? -ne 0 ];then insmod ./nvme.ko
以下是将该 shell 脚本转换为 Python 脚本的代码:
```python
import subprocess
HDDINFO_FILE = "/root/hddinfo.txt"
subprocess.run(["cp", "-rf", "/root/TestPlat/common/slot_sn.ini", INITIALFILE])
with open("/root/TestPlat/common/slot_sn.ini", "r") as f:
G_SCAN_PN = f.readline().split("PN:")[1].split(";")[0].strip()
subprocess.run(["dos2unix", "accurate.txt"])
subprocess.run(["dos2unix", "-f", "accurate.txt"])
with open("accurate.txt", "r") as f:
data = f.read()
poweron_slotbus = "\n".join([line.split("->")[2].split("(")[0].strip() for line in data.split("\n") if "NVME-SLOT-" in line])
poweron_diskbus = "\n".join([line.split("->")[3].split("(")[0].strip() for line in data.split("\n") if "NVME-SLOT-" in line])
with open("poweron_slotbus.txt", "w") as f1:
f1.write(poweron_slotbus)
with open("poweron_diskbus.txt", "w") as f2:
f2.write(poweron_diskbus)
poweron_info = "\n".join([f"{slot}\t{disk}" for slot, disk in zip(poweron_slotbus.split("\n"), poweron_diskbus.split("\n"))])
with open("poweron_info.txt", "w") as f3:
f3.write(poweron_info)
result = subprocess.run(["lsmod"], capture_output=True, text=True)
if "nvme" not in result.stdout:
subprocess.run(["insmod", "./nvme.ko"])
```
注意:该代码仅供参考,因为 shell 脚本和 Python 脚本的语法和执行方式有很大不同,可能需要根据实际情况进行修改。