How to pass value into awk main action block?
by was123 from LinuxQuestions.org on (#5EPD6)
Hello Friends,
I am a new linux learner, recently i was practicing data scraping from yahoo finance.
I fetched the data in tabular form and wanted to find the min and max of each column along with the date.
The scenario is similar to below example.
1-feb-2021 1 2 3 4 5 6 7
2-mar-2020 8 3 4 5 6 4 1
---so on
below is my code.
Quote:
problem: I am not able to pass i value onto $i in awk.
Basically what i want for every iteration of for loop i value should also be passed onto awk so that $i can print the desired field.
like for 1st iteration second field and 2nd iteration 3rd field so on and forth.
I can use cut but i want it with awk to know if there is any possibilities of passing value into awk from outside the awk main action block.
Thanks.


I am a new linux learner, recently i was practicing data scraping from yahoo finance.
I fetched the data in tabular form and wanted to find the min and max of each column along with the date.
The scenario is similar to below example.
1-feb-2021 1 2 3 4 5 6 7
2-mar-2020 8 3 4 5 6 4 1
---so on
below is my code.
Quote:
#!/usr/bin/env bash clear which lynx &>/dev/null if [[ $? -ne 0 ]] then echo "pls install lynx and try again...." exit 1 fi URL="https://in.finance.yahoo.com/quote/GOOG/history?ltr=1" lynx --dump $URL | sed -n '76,175p' | tr -d "," > finance_data.txt if [[ $? -ne 0 ]] then echo "Unable to fetch data from yahoo finance data:" exit 2 fi min_max() { min_value=$(sort -n -k $i finance_data.txt | sed -n '1p' | awk '{print $i}') date_on_min=$(sort -n -k $i finance_data.txt | sed -n '1p' | cut -d " " -f 1 ) max_value=$(sort -nr -k $i finance_data.txt | sed -n '1p' | awk '{print $i}') date_on_max=$(sort -nr -k $i finance_data.txt | sed -n '1p' | cut -d " " -f 1) } for((i=2; i<=7; i++)) do min_max echo -e "$min_value\n$date_on_min" echo -e "$max_value\n$date_on_max" done |
Basically what i want for every iteration of for loop i value should also be passed onto awk so that $i can print the desired field.
like for 1st iteration second field and 2nd iteration 3rd field so on and forth.
I can use cut but i want it with awk to know if there is any possibilities of passing value into awk from outside the awk main action block.
Thanks.