Article 5EPD6 How to pass value into awk main action block?

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:
#!/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
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.latest?d=yIl2AUoC8zA latest?i=v12aLTovt9s:zKpLk4Ta8B0:F7zBnMy latest?i=v12aLTovt9s:zKpLk4Ta8B0:V_sGLiP latest?d=qj6IDK7rITs latest?i=v12aLTovt9s:zKpLk4Ta8B0:gIN9vFwv12aLTovt9s
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments