Taking inputs for awk

Hi, i want to print 2nd column value with the below script. I need to take input of the string i need to search in that file and file name. How can i take these two as inputs? using read command? Getting error for below script.

echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F ";"  /'$SID'/ { print $2 } '$filename'
exit 0

Requesting Help.

echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F ";" -v sid="${SID}" '/sid/ { print $2 }' "${filename}"
exit 0
1 Like

thank you for the help. i'm not getting error now. but i'm not getting the output for this.

can you provide the output of

grep "sid" file

---------- Post updated at 04:58 AM ---------- Previous update was at 04:58 AM ----------

Replace sid and file with respective names

---------- Post updated at 05:03 AM ---------- Previous update was at 04:58 AM ----------

If your SID is A12345 and file name is sample.txt , provide me the output of

 
grep "A12345" samlpe.txt

Try :

$ awk -F';' 'BEGIN{ printf "Enter SID : "; getline sid < "-"; printf "Enter Filename : "; getline file < "-"; while(getline < file){ if($0 ~ sid ) print $2  }close(file) }'

---------- Post updated at 04:38 PM ---------- Previous update was at 04:37 PM ----------

I think it was supposed to be

awk -F ";" -v sid="${SID}" '$0 ~ sid { print $2 }' "${filename}"
1 Like

Small change on awk.

#!/bin/ksh
 
echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F";" '/'"$SID"'/ { print $2 }' ${filename}
exit 0
 

try...

1 Like

srinishoo: ya..first i thought of a simple grep but it fetches both SID and DBName in this format
SID;DBName but i wanted only DBName when the input SID is given. so chose awk which's good in txt processing including table format.

echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F ";" -v sid="${SID}" '$0 ~ sid { print $2 }' "${filename}"
exit 0
1 Like

Akshay & Roozo: Yeah both codes working fine getting the second column i.e., DBName. Thank you very much. But please let me know any doc or URL for awk which teaches me awk step by step.

---------- Post updated at 11:07 AM ---------- Previous update was at 11:04 AM ----------

srinishoo: yeah..got it right with your, Roozo's and Ashkay's awk code. thank you very much.