LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2007, 05:11 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
user input howto using awk


Hello guys, if anyone can tell me how can I modify the script below that would asks users input to print the value...Thanks much...

code:

#!/bin/sh
# ctest.awk

BEGIN{ headingFound = 0; }
/^cquestion:/ { headingFound = 1; }
/^dn:/ {
if( headingFound == 1 ){
print $2;
headingFound = 0;
}
}

# awk -f ctest.awk a.txt > b.txt

currently this script is supposed to ask what the cquestions is like "what is your name?" then next it should print all the dn values from the a.txt file which is a big file and sections of it like:


dn: uid=poi.1111,ou=abc,o=cc.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: xyf88561
dn: uid=poi.2222,ou=def,o=dd.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: leo210375
 
Old 09-25-2007, 08:22 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you had a similar thread like this few days ago. Read post #18 from that thread. That's how you get user input inside awk.
 
Old 09-26-2007, 08:14 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
couldnt me it run yet

I really appreciate for the help. I am still trying to figure out why I couldnt make it run. When I run this: awk -f gtest.awk a.txt I get an error ,...perhaps something I did wrong in the script...please have a look if you get a min...I would really appreciate...

code:

awk 'BEGIN{ headingFound = 1;
print "what is cquestion? "
getline choice < "/dev/tty"
}
}

/^cquestion:/ { headingFound = 1; }

/^dn:/ {
if( headingFound == 1 ){
print $2;
headingFound = 0;
}

}






Quote:
Originally Posted by ghostdog74 View Post
you had a similar thread like this few days ago. Read post #18 from that thread. That's how you get user input inside awk.
 
Old 09-26-2007, 08:55 AM   #4
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
couldnt make it run yet

I really appreciate for the help. I am still trying to figure out why I couldnt make it run. When I run this: awk -f gtest.awk a.txt I get an error ,...perhaps something I did wrong in the script...please have a look if you get a min...I would really appreciate...

code:

awk 'BEGIN{ headingFound = 1;
print "what is cquestion? "
getline choice < "/dev/tty"
}
}

/^cquestion:/ { headingFound = 1; }

/^dn:/ {
if( headingFound == 1 ){
print $2;
headingFound = 0;
}

}






Quote:
Originally Posted by ghostdog74 View Post
you had a similar thread like this few days ago. Read post #18 from that thread. That's how you get user input inside awk.
 
Old 09-26-2007, 10:07 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi, cmontr!

If I understand your question correctly, then you can try this:
Code:
# Why you write /bin/sh here? You could write
#   #!/bin/awk -f
# and run $ chmod +x ctest.awk
# instead to make this script executable

# ctest.awk

BEGIN{
	headingFound = 0;
	print "what is your name?: "
	getline choice < "/dev/tty"
}
/^cquestion:/ { if($2==choice) headingFound = 1; }
/^dn:/ {
	if( headingFound == 1 ){
		print $2;
		headingFound = 0;
	}
}
This will find first occurrence of '^cquestion: <choice>'
and then print first field '^dn: ...' which it found.

Hope this helps.
 
Old 09-26-2007, 03:24 PM   #6
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
thanks but still getting error

Hi again, thanks for the help but I am getting error running the script against a file called a.txt.

It indicates:

awk: syntax error near line 5
awk: illegal statement near line 5 --> where this line is as follows:


1 #!/bin/awk -f
2 BEGIN{
3 headingFound = 0;
4 print "what is your name?: "
5 getline choice < "/dev/tty"
6 }
7 /^cquestion:/ { if($2==choice) headingFound = 1; }
8 /^dn:/ {
9 if( headingFound == 1 ){
10 print $2;
11 headingFound = 0;
12 }
13 }

I know all you guys directed as much but still getting some errors as seen. Can you please direct me again? Thanks much.


Quote:
Originally Posted by firstfire View Post
Hi, cmontr!

If I understand your question correctly, then you can try this:
Code:
# Why you write /bin/sh here? You could write
#   #!/bin/awk -f
# and run $ chmod +x ctest.awk
# instead to make this script executable

# ctest.awk

BEGIN{
	headingFound = 0;
	print "what is your name?: "
	getline choice < "/dev/tty"
}
/^cquestion:/ { if($2==choice) headingFound = 1; }
/^dn:/ {
	if( headingFound == 1 ){
		print $2;
		headingFound = 0;
	}
}
This will find first occurrence of '^cquestion: <choice>'
and then print first field '^dn: ...' which it found.

Hope this helps.
 
Old 09-26-2007, 11:39 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi!

Maybe the reason is your awk interpreter? Please post the output of `awk --version' as well as your distribution (I use Debian for example and GNU awk 3.1.5). You also can omit '< "/dev/tty"' from call to getline, because it is useless IMO and your awk may not understand redirection. If that did not work you can try getline without arguments, and then assign the value of $0 to choice. And if that will not work... ok, just use BASH features:
Code:
#!/bin/bash
# USAGE: ctest.sh a.txt

echo -n "what is your name?: "
read choice
awk "
BEGIN{
	headingFound = 0;
}
/^cquestion:/ { if(\$2==\"$choice\") headingFound = 1; }
/^dn:/ {
	if( headingFound == 1 ){
		print \$2;
		headingFound = 0;
	}
}
" $1
or try Perl solution.

Good luck!
 
Old 09-27-2007, 01:28 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by firstfire View Post
You also can omit '< "/dev/tty"' from call to getline, because it is useless IMO and your awk may not understand redirection.
not really. consider this
Code:
bash-2.03$ more file
line1
line2
line3
bash-2.03$ nawk 'BEGIN{getline  choice}{print $0,choice}' "file"
line2 line1
line3 line1
bash-2.03$ nawk 'BEGIN{getline  choice<"/dev/tty"}{print $0,choice}' "file"
test
line1 test
line2 test
line3 test
if "/dev/tty" is not used, the first $0 is "gobbled" up.
@OP, if your system has nawk, you can try that and see.
 
Old 09-27-2007, 08:09 AM   #9
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
pls look again

I am sorry - I guess you misunderstood.

Script should ask a question when you run, such as:

"What is your vzquestion?" And it would go get the value and print of dn of whenever it finds that cquestion in the txt file from db.

If you can help, that would greatly appreciate it.

Thank you again

------------------

Quote:
Originally Posted by cmontr View Post
Hi again, thanks for the help but I am getting error running the script against a file called a.txt.

It indicates:

awk: syntax error near line 5
awk: illegal statement near line 5 --> where this line is as follows:


1 #!/bin/awk -f
2 BEGIN{
3 headingFound = 0;
4 print "what is your name?: "
5 getline choice < "/dev/tty"
6 }
7 /^cquestion:/ { if($2==choice) headingFound = 1; }
8 /^dn:/ {
9 if( headingFound == 1 ){
10 print $2;
11 headingFound = 0;
12 }
13 }

I know all you guys directed as much but still getting some errors as seen. Can you please direct me again? Thanks much.
 
Old 09-27-2007, 11:25 PM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by ghostdog74 View Post
not really.
...
if "/dev/tty" is not used, the first $0 is "gobbled" up.
@OP, if your system has nawk, you can try that and see.
Yes, you right, ghostdog74, sorry.

cmontr, please post some info about your environment, at least version of your awk interpreter. On my system all mentioned scripts work well without error messages and some of them do exactly what you want.

If the reason is in `getline' statement or not, this can be checked simply by using `choice="..."' instead of `getline'.
 
Old 09-28-2007, 08:02 AM   #11
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
in my system awk --version doent bring anything ...it is SUN ultra 250 running solaris 5.7 and oracle 9i db. Please let me know what commands you like m to run for a specific information.
 
Old 09-29-2007, 07:47 AM   #12
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi!

On this site I found that your version of awk uses very restricted syntax of `getline' statement:
Code:
getline 

Set $0 to the next input record from the current input file. getline returns 1 for successful input, 0 for end of file, and -1 for an error.
.. and there is nothing about redirection (I mean this `<"/dev/tty"').

But there is another man page describing `nawk' (man nawk) which is placed in /usr/bin/nawk or /usr/xpg4/bin/awk. nawk seems to possess all the features we need, so try it.

Anyway you can just pass the variable `choice' to the interpreter:

Code:
$ nawk -v 'choice=...' -f ctest.awk a.txt
and use a shell script to obtain actual value for `choice'.

P.S.: If "/dev/tty" do not work for you, try "/dev/stdin", it should be there I think.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make extra stdin input in awk ? khaan Programming 3 07-30-2007 05:04 AM
How add input value to some varieble in awk program this is probably funny for some of you but I do not know what to do. I have sarajevo Programming 2 05-25-2007 07:54 AM
Xorg input driver module howto CrazyGenie Linux - Desktop 0 12-13-2006 11:53 PM
Howto write XF86 Input Module? ZX_SA Programming 0 05-11-2005 09:11 AM
Howto disable user input on the console? jpan Programming 15 08-13-2004 01:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration