我有一个csv文件,格式如下。
event1,event2,event3,event4
"Ozz","time","1234","test1"
"Max","time1","5678","test2"
我需要替换第一列值而不更改标题。 我需要将第二列值复制到第三列。 所以预期的输出是:
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
输出将是另一个csv文件 我不知道该如何解决这个问题?
答案 0 :(得分:1)
您可以使用类似
的内容awk 'BEGIN{FS=OFS=","} $1~/^".*"$/{gsub("^\"Ozz\"$", "\"Razor\"",$1); gsub("^\"Max\"$","\"Maxine\"", $1); $3=$2;print $0} /event/' inputFile
输出
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
答案 1 :(得分:1)
如果文件很小(少于100行),使用纯bash是一个不错的选择:
exec 3<inputfile
read -ru 3 a; echo $a
while IFS=, read -ru 3 v a b b; do
v=${v/#\"/};v=${v/%\"/} # Trim surrounding "
case $v in
Ozz) v=Razor;;
Max) v=Maxine;;
*) echo "Wrong value '$v'">&2; v=Default;;
esac
echo "\"$v\",$a,$a,$b"
done
输出:
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
替换可以从另一个文件中读取,并且可以使用散列进行替换。