Skip to main content
Added OFS flag to awk for better management of output format
Source Link
KM.
  • 2.3k
  • 2
  • 19
  • 26

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column of first file with first column of second (think "keys"); pipe it to awk and print reordered columns, using , as field separator (-F) and as the Output Field Separator (-OFS)

join -t ','  -1 1 -2 1 filename sum | awk -F, -OFS=, {'print $1"$1," $4"$4," $2"$2," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column of first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column of first file with first column of second (think "keys"); pipe it to awk and print reordered columns, using , as field separator (-F) and as the Output Field Separator (-OFS)

join -t ','  -1 1 -2 1 filename sum | awk -F, -OFS=, {'print $1,$4,$2,$3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2
deleted 1 character in body
Source Link
KM.
  • 2.3k
  • 2
  • 19
  • 26

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column forof first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column for first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column of first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2
added 4 characters in body
Source Link
KM.
  • 2.3k
  • 2
  • 19
  • 26

Save the sum to a file called "sum"sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum 

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column for first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filefilename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called "sum", sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum

Join the two files, first column for first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 file sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2

Save the sum to a file called sum, sorted

awk -F, '{a[$1]+=$3;}END{for (i in a)print i", "a[i];}' filename | sort > sum 

cat sum 
product_a, 515
product_b, 348
product_c, 495
product_d, 27

Join the two files, first column for first file with first column of second (think "keys"); pipe it to awk and print reordered columns

join -t ','  -1 1 -2 1 filename sum | awk -F, {'print $1"," $4"," $2"," $3}'

product_a,  515,  domestic,  500
product_a,  515,  abroad,    15
product_b,  348,  domestic,  313
product_b,  348,  abroad,    35
product_c,  495,  domestic,  411
product_c,  495,  abroad,    84
product_d,  27,   domestic,  25
product_d,  27,   abroad,    2
Added explanation of commands; fixed formatting and removed last comma
Source Link
KM.
  • 2.3k
  • 2
  • 19
  • 26
Loading
Source Link
KM.
  • 2.3k
  • 2
  • 19
  • 26
Loading