【问题标题】:how do I replace a information Fromm csv file?如何替换 csv 文件中的信息?
【发布时间】:2022-10-25 14:14:15
【问题描述】:

我有以下程序

#!/bin/bash
exec 3< lista.csv
read -u 3 header
declare -i id_nou
echo "ID: "
read id_nou
while IFS=, && read -u 3 -r id nume prenume seria grupa nota
do
if [ "$id_nou" -eq "$id" ]
then
echo "Nota noua: "
read  nota_noua
nota=$nota_noua
print > lista.csv
fi
done

我的 csv 文件看起来像这样:

id,nume,prenume,grupa,seria,nota

1,Ion,Andrada,1003,A,8

2,Simion,Raluca,1005,A,7

3,Gheorghita,Mihail,1009,B,5

4,Mihailescu,Georgina,1002,A,6

我想要做的是用键盘值给定的替换通讯员 id 的 nota 值,但这似乎不起作用。 错误信息是

第 14 行:打印:找不到命令

【问题讨论】:

  • print 不是 bash 的内置程序。你想用它做什么? zsh 有一个同名的内部命令。
  • 好的,那我可以写什么呢?
  • 你想达到什么效果?
  • 好吧,我的 csv 文件具有以下格式:id、name、surname、group、serie、grade。我想更改给定 id 的等级值
  • 请发布一些带有相关预期输出的示例数据。不要将它们作为 cmets、图像、表格或非现场服务的链接发布,而是使用文本并将它们包含在您的原始问题中。谢谢。

标签: bash csv


【解决方案1】:

这是awk中的一个:

awk 'BEGIN {
    FS=OFS=","                            # comma field separators
    printf "id: "                         # ask for id
    if((getline id < "/dev/stdin")<=0)    # store to a variable
        exit 1
    printf "nota: "                       # ...
    if((getline nota < "/dev/stdin")<=0)
        exit 1
}
$1==id {                                  # if firsst field matches
    $NF=nota                              # replace last field value
}
1' file                                   # output

输出:

id: 1
nota: THIS IS NEW VALUE
id,nume,prenume,grupa,seria,nota
1,Ion,Andrada,1003,A,THIS IS NEW VALUE
2,Simion,Raluca,1005,A,7
3,Gheorghita,Mihail,1009,B,5
4,Mihailescu,Georgina,1002,A,6

Here 是有关保存更改的一些信息。

【讨论】:

  • 我还没有弄清楚如何更改更改,请您帮帮我吗?
  • 如果您使用的是 GNU awk:gawk -i inplace ...,否则为 awk ... file &gt; tmp &amp;&amp; mv tmp file
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2021-11-20
  • 2010-11-12
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多