Timeline for Use for loop variable in bash for other command
Current License: CC BY-SA 4.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 19, 2020 at 0:55 | comment | added | 9f241e21 | @EdMorton Thanks for your reply. I agree that my solution is very inefficient. I'll check based on your suggestions | |
| Feb 18, 2020 at 18:02 | comment | added | Ed Morton |
@9f241e21 You don't need a shell loop since awk can do the whole thing on it's own, and even if you did need a loop for some reason you don't need to pipe the awk output to wc since awk can count lines for itself too and given that you don't need awk to print $NF. Also, awk is made up of condition { action } statements so you shouldn't put an explicit condition inside the action part while leaving an implicit true condition to execute it. You got an answer to the question you asked but if you'd like help to do whatever it is you're trying to do the right way then post a new question.
|
|
| Feb 18, 2020 at 13:18 | history | edited | AdminBee | CC BY-SA 4.0 |
Correct erroneous statement as pointed out by Stéphane Chazelas
|
| Feb 18, 2020 at 13:06 | vote | accept | 9f241e21 | ||
| Feb 18, 2020 at 13:03 | comment | added | 9f241e21 |
Thanks for your reply. The intention is to run a for loop, each loop do awk '{if ($NF==$i) {print $NF}}' * | wc -l, where i from 1 to 10. I find that change to count=awk -v fieldnr=$i '{if ($NF==fieldnr) {print $NF}}' * | wc -l does the job. Your solution is very helpful!
|
|
| Feb 18, 2020 at 13:00 | history | edited | AdminBee | CC BY-SA 4.0 |
Add more explanation
|
| Feb 18, 2020 at 12:54 | history | answered | AdminBee | CC BY-SA 4.0 |