Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk -- user input 3

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
Hello All

I am running a awk script which looks like this

if( a && $1 = "51264")
{
...
}

The value 51264 may vary.
So how do I ask the user to input the value for the above program.

Like in shell scripting we perform

echo " Enter the number"
read num
and substitute num.


Regards
MP
 
I have used the -v param to set a variable at the command line. Then I build a shell script wrapper around the awk call to collect the user input.

Like this:
script.sh
Code:
echo "Enter the number: \c"
read num
awk -v number=$num script.awk filename
and the awk would be:
script.awk
Code:
...
if ( a && $1 = number ) {
...
}
I know this will work, however it isn't the most elegant method. Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
use your favorit shell to prompt for 'num' and call nawk as:

nawk -v num=${num} myAwk.awk

#-------------- myAwk.awk

if( a && $1 == num)
{
...
}
#-------------- myAwk.awk
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Or entirely in awk - your 'input stream' [&quot;-&quot;] might vary based on the underlying OS. No error/type checking is provided.


# ----------------------------------------------
printf &quot;Enter enter your number> &quot;
getline num<&quot;-&quot;
#print &quot;Entered text=&quot; num


if( a && $1 == num)
{
...
}

# ---------------------------------------------- vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hello Vlad
Very very helpful post. Thanks to all awk forum experts for solving my problems.
But now I have one more problem.

If I do

if( a && $1 == 51264)
{
...
}
and run my program I get a long output which I pipe it in to another file.

myawkscript process_file > output file

But in the below way

printf &quot;Enter enter your number> &quot;
getline num<&quot;-&quot;

if( a && $1 == num)
{
...
}

myawkscript process_file

I need to stop above so that the user enters his input and then how can I pipe the output to another file?

regards
MP
 
Without having time to figure out the 'all-awk' solution with output redirection, use the FIRST proposed solution [shell wrapper with variable assignment] and redirect awk's output as you've done originally.

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Actually I was trying the shell wrapper with variable assignment by using below way..I was not succesful and posted the question again..
echo &quot;Enter the number: \c&quot;
read num
awk -v number=$num script.awk filename

But now I figured where was the problem...
-f parameter was not passed.

awk -v number=$num -f script.awk filename > output file

Now it works

Thanks
MP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top