Skip to main content
edited body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }' test2.txt

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }' test2.txt

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value, you will get an error, or if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }' test2.txt

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

added 10 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }' test2.txt

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }'

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }' test2.txt

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

added 379 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }'

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }'

The reason this does not work the way you have written it is that awk will interpret $i as "the ith field", and since the awk variable i has no value,you will get an error, or, if you are using GNU awk or mawk, $i will be the same as $0 which is the whole line (with mawk or GNU awk, the program looks for lines whose first column is the same as the whole line).

Instead, to "import" you shell variable into awk:

awk -v i="$i" '$1 == i { sum += $4 } END { print sum }'

Also, the value of the shell variable $i will only ever be the name of the file test1.txt (since this is what you loop over).

To loop over the contents of the file:

while IFS= read -r i; do
    awk ...as above...
done <test1.txt

αғsнιη's answer shows how you can do this without using a shell loop.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading