Taking inputs for awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking inputs for awk
# 1  
Old 07-09-2014
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.
# 2  
Old 07-09-2014
Code:
echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F ";" -v sid="${SID}" '/sid/ { print $2 }' "${filename}"
exit 0

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 07-09-2014
thank you for the help. i'm not getting error now. but i'm not getting the output for this.
# 4  
Old 07-09-2014
can you provide the output of
Code:
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
Code:
 
grep "A12345" samlpe.txt

# 5  
Old 07-09-2014
Try :
Code:
$ 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 ----------

Quote:
Originally Posted by SriniShoo
Code:
echo "enter SID"
read SID
echo "enter filename"
read filename
awk -F ";" -v sid="${SID}" '/sid/ { print $2 }' "${filename}"
exit 0

I think it was supposed to be

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

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 07-09-2014
Small change on awk.

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

try...
This User Gave Thanks to Roozo For This Post:
# 7  
Old 07-11-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking nth column and putting its value in n+1 column using awk

Hello Members, Need your expert opinion how to tackle below. I have an input file that looks like below: USS|AWCC|AFGAW|93|70 USSAA|Roshan TDCA|AFGTD|93|72,79 ALB|Vodafone|ALBVF|355|69 ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550 I like output file in below format: ... (7 Replies)
Discussion started by: umarsatti
7 Replies

2. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

3. Shell Programming and Scripting

awk command line arguments not taking

# more minusf.awk #!/bin/awk -f BEGIN { FS=":"; } { if ( $2 == "" ) { print $1 ": no password!"; } } # ./minusf.awk aa aa aa aa awk: can't open aa (6 Replies)
Discussion started by: sri.phani
6 Replies

4. Shell Programming and Scripting

Cannot get the correct ans. Using awk in taking average

Hi all, I think so I’m getting the result is wrong, while using following awk commend, colval=$(awk 'FNR>1 && NR==FNR{a=$4;next;} FNR>1 {a+=$4; print $2"\t"a/3}' filename_f.tsv filename_f2.tsv filename_f3.tsv) echo $colval >> Result.tsv it’s doing the condition 2 times, first result... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

5. Shell Programming and Scripting

Taking inputs on prompt

need a help to get a script: bash # ./xx.sh >> count 567 script will run the xx.sh and it will go to >> then run "count" will get the result "567" and print it (7 Replies)
Discussion started by: Aditya.Gurgaon
7 Replies

6. UNIX for Dummies Questions & Answers

awk solution for taking bins

Hi all, I'm looking for an awk solution for taking bins of data set. For example, if I have two columns of data that I wish to use for a scatter plot, and it contains 5 million lines, how can I take averages of every 100 points, 1000, 10000 etc... The idea is to take bins of the 5,000,000 points... (7 Replies)
Discussion started by: torchij
7 Replies

7. Shell Programming and Scripting

Give inputs externally to awk script

Hello to all, Please some awk expert could help me. If I want to run an awk script as "command" give it inputs externally I do: Script.sh Input="$1" # "$1" is the input that will be given to the script Output=${Input%.*}.csv awk '{$1=$1}1' $Input | awk '{...}' > $Output and I run the... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

8. UNIX for Dummies Questions & Answers

taking the output of awk command to a new file

cat doc | nawk -v da="${date}" '$23>199 {print $0 > "doc"+da+".txt"}' Every time(need to run every day) i run this, i want to a create a new file "doc_01 Aug.txt". Basically, i want to create a new file with date appended in it. The above command is creating a file with name "0".... (4 Replies)
Discussion started by: vagar11
4 Replies

9. Shell Programming and Scripting

Perl script for taking inputs from one script and storing them into a document.

Hi. I wanted to create a Perl script which can take the outputs of a Perl script as it's input and temporarily store them in a document. Need help. Thanks.:) (8 Replies)
Discussion started by: xtatic
8 Replies

10. Shell Programming and Scripting

Bash/AWK Newbie taking on more than he can chew.

A few questions: I'm trying to use Bash (although I'm not against using AWK) to try to accomplish a few things, but I'm stumped on a few points. I'm learning most of the basics quickly: but there are a few things I can't figure out. 1. I'm trying to count the number of .txt files in a... (3 Replies)
Discussion started by: Asylus
3 Replies
Login or Register to Ask a Question