【问题标题】:Add headings to the column data in csv file linux为csv文件linux中的列数据添加标题
【发布时间】:2020-04-11 17:14:33
【问题描述】:
t_apps=`cat $output1 | awk '{print $2,$3,$4}'| uniq | wc -l`
t_instances=`cat $output1 | wc -l`
r_apps=`cat $output4 | awk '{print $2,$3,$4}'| uniq | wc -l`
n_apps=`cat $output5 | awk '{print $2,$3,$4}'| uniq | wc -l`
n_instances=`cat $output2 | wc -l`
d_instances=`cat $output3 | wc -l`
echo "$time,$t_apps,$n_apps,$r_apps,$t_instances,$n_instances,$d_instances" | sed 's/,/  /g' | >> /tmp/c_data.csv

尝试将静态标题添加到文件 c_data.csv。

谁能帮帮我。

例如

csv 文件中的数据如下,

20 30 100 230

我想在列数据中添加标题

tapp napp rapp tinstances

20 30 100 250

更新到 csv 文件数据后应该是这样的,列标题应该是静态的

tapp napp rapp tinstances

20 30 100 250

21 35 150 100

【问题讨论】:

  • 很好,你已经展示了你的代码努力,唯一的事情是将你的示例和代码包装到代码标签中,然后让我们知道。

标签: linux shell awk sed


【解决方案1】:
{
  echo "tapp napp rapp tinstances"
  echo "20 30 100 250"

  # replace this line with your code
  echo "21 35 150 100"
} | column -t > data.csv

| column -t 用于将列格式化为表格格式。如果您不需要它,请将其删除。

【讨论】:

  • 如果我写了两次 echo 如果我运行脚本会再次打印标题。但我不想使用相同的脚本再次打印标题。只想用相同的脚本更新列数据(相同的脚本运行多次)。
  • 你没有。您替换 的行是 您的脚本,我写的是您的第二个脚本:{ echo "headline..."; ./script.sh; } | column -t > data.csv 或 { echo "headline..."; ./script.sh | tail -n+3; } | column -t > data.csv 如果您需要替换前两行。
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多