Reputation: 3
I have a csv file with the following format.
event1,event2,event3,event4
"Ozz","time","1234","test1"
"Max","time1","5678","test2"
I need to replace the first columns value without changing the header. And I need the second columns value copied in to the third column. So the expected output is:
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
Output will be to another csv file I am not sure how to go about this ?
Upvotes: 0
Views: 304
Reputation: 26687
You can use somthing like
awk 'BEGIN{FS=OFS=","} $1~/^".*"$/{gsub("^\"Ozz\"$", "\"Razor\"",$1); gsub("^\"Max\"$","\"Maxine\"", $1); $3=$2;print $0} /event/' inputFile
giving outpu as
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
Upvotes: 1
Reputation: 7610
If the file is small (less then 100 lines) using pure bash is a good alternative:
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
Output:
event1,event2,event3,event4
"Razor","time","time","test1"
"Maxine","time1","time1","test2"
The replacement could be read from another file and a hash could be used for the replacement.
Upvotes: 1