【问题标题】:Bash get-hardware script output to .csv filebash 获取硬件脚本输出到 .csv 文件
【发布时间】:2023-03-24 03:15:02
【问题描述】:

我正在使用以下脚本来获取服务器硬件信息:

https://pastebin.com/5Um4Bir6

下面的代码只是其中的一部分(正如 StackOverflow 推荐的那样)

echo
echo -e "+++++++++++++++++++ \e[1;34m SYSTEM \e[0m+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c system -short |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |grep -v "=" |grep -v Desc |grep -v PNP |sed 's/system //g'
echo
echo -e "+++++++++++++++++++ \e[1;34m CPU \e[0m +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c processor -short |grep -v "=" |grep -v Desc |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |sed 's/processor //g'
echo
echo -e "+++++++++++++++++++ \e[1;34m MEMORY \e[0m ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c memory -short |grep -v "=" |grep -v Desc |grep -v empty |grep -v cache |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |sed 's/memory //g' |grep DIMM

我在 Ubuntu 18.04.03 和 CentOS 7 机器上使用此脚本,我需要帮助才能将输出转换为 .csv 文件。 并且输出应该像具有 SYSTEM、CPU、MEMORY 等的标题。相应的列应该有 lshwethtoolnvidia-smi 等命令的输出。

【问题讨论】:

  • Just >> out.csv 但是你的问题不正确。为什么要将一半的​​回声放入 csv 中?你想“保存”什么?
  • 我需要保存标题,它有 SYSTEM、CPU、MEMORY 等。以及相应列中的 lshw、ethtool 等命令的输出。
  • 一般grep | grep | sed | grep | cut是一种反模式,最好用awk表达

标签: linux bash shell ubuntu centos


【解决方案1】:

尝试通过使用大括号 { }>> 重定向来使用命令分组。

{
echo
echo -e "+++++++++++++++++++ \e[1;34m SYSTEM \e[0m+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c system -short |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |grep -v "=" |grep -v Desc |grep -v PNP |sed 's/system //g'
echo
echo -e "+++++++++++++++++++ \e[1;34m CPU \e[0m +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c processor -short |grep -v "=" |grep -v Desc |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |sed 's/processor //g'
echo
echo -e "+++++++++++++++++++ \e[1;34m MEMORY \e[0m ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
lshw -c memory -short |grep -v "=" |grep -v Desc |grep -v empty |grep -v cache |sed 's/ \{1,\}/ /g'| cut -d' ' -f2-100 |sed 's/memory //g' |grep DIMM
} >> file.csv

【讨论】:

  • 谢谢!这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2011-03-25
  • 2020-03-22
相关资源
最近更新 更多