Skip to main content
added 159 characters in body
Source Link

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.dat | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.dat

I've taken your file formats literally, if you do actually require true CSVs then a small mod will accomplish that.

cat in.csv | awk -F"," '{if(NR==1)hdr=$0; else{emp[$1","$2]=emp[$1","$2]+$3}} END{OFS=",";print hdr; for(i in emp) print i,emp[i];}' > out.csv

Name,Age,Salary

name 2,55,20066

name 1,52,110322

name 3,50,100054

Designed as a text manipulation language by some very clever computer scientists, including Brian Kernighan of C fame, AWK achieves this compactness by automatically parsing any input it is given into fields ( \$1, \$2, ... ) and keeping track of the number of lines processed. This allows a lot of the I/O management you might need to do in another language to be skipped over as it already exists.

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.dat | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.dat

I've taken your file formats literally, if you do actually require true CSVs then a small mod will accomplish that.

cat in.csv | awk -F"," '{if(NR==1)hdr=$0; else{emp[$1","$2]=emp[$1","$2]+$3}} END{OFS=",";print hdr; for(i in emp) print i,emp[i];}' > out.csv

Name,Age,Salary

name 2,55,20066

name 1,52,110322

name 3,50,100054

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.dat | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.dat

I've taken your file formats literally, if you do actually require true CSVs then a small mod will accomplish that.

cat in.csv | awk -F"," '{if(NR==1)hdr=$0; else{emp[$1","$2]=emp[$1","$2]+$3}} END{OFS=",";print hdr; for(i in emp) print i,emp[i];}' > out.csv

Name,Age,Salary

name 2,55,20066

name 1,52,110322

name 3,50,100054

Designed as a text manipulation language by some very clever computer scientists, including Brian Kernighan of C fame, AWK achieves this compactness by automatically parsing any input it is given into fields ( \$1, \$2, ... ) and keeping track of the number of lines processed. This allows a lot of the I/O management you might need to do in another language to be skipped over as it already exists.

added 159 characters in body
Source Link

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.csv | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$$> cat in.dat | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.csvdat

I've taken your file formats literally, if you do actually require true CSVs then a small mod will accomplish that.

cat in.csv | awk -F"," '{if(NR==1)hdr=$0; else{emp[$1","$2]=emp[$1","$2]+$3}} END{OFS=",";print hdr; for(i in emp) print i,emp[i];}' > out.csv

Name,Age,Salary

name 2,55,20066

name 1,52,110322

name 3,50,100054

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.csv | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.csv

I've taken your file formats literally, if you do actually require CSVs then a small mod will accomplish that.

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.dat | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.dat

I've taken your file formats literally, if you do actually require true CSVs then a small mod will accomplish that.

cat in.csv | awk -F"," '{if(NR==1)hdr=$0; else{emp[$1","$2]=emp[$1","$2]+$3}} END{OFS=",";print hdr; for(i in emp) print i,emp[i];}' > out.csv

Name,Age,Salary

name 2,55,20066

name 1,52,110322

name 3,50,100054

Source Link

This isn't a python program so strictly it isn't a solution to your problem, technically it isn't even a program - just a command line, but it does illustrate that are many ways to skin a cat, some of them quite elegantly.

Dictionaries are a lovely feature of the modern language python but much earlier languages such as AWK, or it's linux equivalent GAWK, have had them for just short of 40 years, though in those days they were called associative arrays.

So here is a one line command using cat and gawk, with its ability to accept code as an argument, to accomplish your task, demonstrating that the design principle of linux to be a framework for collaborating utilities is still alive and well.

 $> cat in.csv | gawk '{if(NR==1)hdr=$0; else{emp[$1" "$2" "$3]=emp[$1" "$2" "$3]+$4}} END{print hdr; for(i in emp) print i,emp[i];}'  > out.csv

I've taken your file formats literally, if you do actually require CSVs then a small mod will accomplish that.