Skip to main content
Rollback to Revision 1
Source Link
Prabhjot Singh
  • 2.4k
  • 1
  • 6
  • 20

Using awk:

awk -F',' 'BEGIN{a=0}'{p[a] p[NR] = $1;net[a]$1;net[NR] = $2+$3-$4; ++a$4} END {for (i=1;i<NR;i++i=2;i<=NR;i++) print (i-1), p[i], "net = ", net[i]; net[i]}' data2data.csv

Because this is a csv file, FS = ',' or -F ','

In BEGIN block index value for array is set to zero. Next p and net arrays are created. These are indexed on NR(record numbers).

Then a for loop print (i-1), p[i], "net = ", net[i];net[i] works as you planned. The size of loop is less than NR (i<NR)for because arrays start from zero(for loop is being started form one)from 2 because first line is header line.

Thispipe in this case is similar to array indexed on NR(record numbers)useless use. Anohter thing your loop is good but limit should be NR not NF.

Using awk:

awk -F',' 'BEGIN{a=0}{p[a] = $1;net[a] = $2+$3-$4; ++a} END {for (i=1;i<NR;i++) print i, p[i], "net = ", net[i]; }' data2.csv

Because this is a csv file, FS = ',' or -F ','

In BEGIN block index value for array is set to zero. Next p and net arrays are created.

Then a for loop print i, p[i], "net = ", net[i]; works as you planned. The size of loop is less than NR (i<NR) because arrays start from zero(for loop is started form one).

This is similar to array indexed on NR(record numbers).

Using awk:

awk -F',' '{ p[NR] = $1;net[NR] = $2+$3-$4} END {for (i=2;i<=NR;i++) print (i-1), p[i], "net = ", net[i]}' data.csv

Because this is a csv file, FS = ',' or -F ','

Next p and net arrays created. These are indexed on NR(record numbers).

Then a for loop (i-1), p[i], "net = ", net[i] works as you planned. for loop is being started from 2 because first line is header line.

pipe in this case is useless use. Anohter thing your loop is good but limit should be NR not NF.

added 14 characters in body
Source Link
Prabhjot Singh
  • 2.4k
  • 1
  • 6
  • 20

Using awk:

awk -F',' ''BEGIN{ p[NR]a=0}{p[a] = $1;net[NR]$1;net[a] = $2+$3-$4$4; ++a} END {for (i=2;i<=NR;i++i=1;i<NR;i++) print (i-1), p[i], "net = ", net[i]net[i]; }' datadata2.csv

Because this is a csv file, FS = ',' or -F ','

In BEGIN block index value for array is set to zero. Next p and net arrays created. These are indexed on NR(record numbers)created.

Then a for loop (print i-1), p[i], "net = ", net[i]net[i]; works as you planned. The size of loop is less than NR for(i<NR) because arrays start from zero(for loop is being started from 2 because first line is header lineform one).

pipe in this case is useless use. Anohter thing your loop This is good but limit should be NR not NFsimilar to array indexed on NR(record numbers).

Using awk:

awk -F',' '{ p[NR] = $1;net[NR] = $2+$3-$4} END {for (i=2;i<=NR;i++) print (i-1), p[i], "net = ", net[i]}' data.csv

Because this is a csv file, FS = ',' or -F ','

Next p and net arrays created. These are indexed on NR(record numbers).

Then a for loop (i-1), p[i], "net = ", net[i] works as you planned. for loop is being started from 2 because first line is header line.

pipe in this case is useless use. Anohter thing your loop is good but limit should be NR not NF.

Using awk:

awk -F',' 'BEGIN{a=0}{p[a] = $1;net[a] = $2+$3-$4; ++a} END {for (i=1;i<NR;i++) print i, p[i], "net = ", net[i]; }' data2.csv

Because this is a csv file, FS = ',' or -F ','

In BEGIN block index value for array is set to zero. Next p and net arrays are created.

Then a for loop print i, p[i], "net = ", net[i]; works as you planned. The size of loop is less than NR (i<NR) because arrays start from zero(for loop is started form one).

This is similar to array indexed on NR(record numbers).

Source Link
Prabhjot Singh
  • 2.4k
  • 1
  • 6
  • 20

Using awk:

awk -F',' '{ p[NR] = $1;net[NR] = $2+$3-$4} END {for (i=2;i<=NR;i++) print (i-1), p[i], "net = ", net[i]}' data.csv

Because this is a csv file, FS = ',' or -F ','

Next p and net arrays created. These are indexed on NR(record numbers).

Then a for loop (i-1), p[i], "net = ", net[i] works as you planned. for loop is being started from 2 because first line is header line.

pipe in this case is useless use. Anohter thing your loop is good but limit should be NR not NF.