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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello experts. I have a problem 1

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Hello experts.

I have a problem with getting input from getline into
the main portion of this script..

slist | awk ' {
$0 = $1
split($0,server,"\n")
print server[1]
print "SAVE(y|n): "
getline choice < &quot;-&quot;
if (choice == &quot;yes&quot;) {
print server[1] >> &quot;/tmp/foo&quot;
}'

(for testing only-the original was
awk '
function get_in(arr, temp,choice,num) {
temp = &quot;/tmp/foo&quot;
getline choice < &quot;-&quot;
if (choice != &quot;no&quot;) {
print arr[num] >> temp
}
}
{
main
}
I would rather do this with awk, than have to write it in
expect or the shell since the data is being generated
this way easily.

Thanks.
 
Hi marsd,

This is from an old thread. I thought it might help.

printf &quot;Enter your name: &quot;
system(&quot;read a; echo $a&quot;) | getline name
print name

I hope this may help someone else.


The execution of an awk script takes place in a child
shell. This makes it difficult to both read and display
the contents of a file and handle interactive queries
about the data contained in the file or files.

I would suggest either Perl or C to do what you are
asking. You need something that executes in the
current shell that will handle multiple data streams.

I will work on this, but you should know that it won't
be an easy task.



flogrr
flogr@yahoo.com

 
Forget it flogrr. I just thought it was me.
I did see an awk script in the o'reilly book
for awk and sed that seemed to approximate what
I am doing...
I only know expect, awk and the shell well enough
to try this in, and awk is the best at pattern
matching and string manipulation, so..
Thanks.
 
Hi marsd,

This does what you want (I think).

It was quite easy as it turns out!

Perhaps, I was thinking of something else.


nawk 'BEGIN{FS=&quot;&quot;} # sets record to one field
{
print &quot;&quot;
print $1 &quot; ?&quot;
print &quot;&quot;
printf &quot;SAVE(y/n): &quot;
getline choice < &quot;-&quot;

if (choice == &quot;yes&quot;) {
print $1 >> &quot;/tmp/foo&quot;
} else {
next
}

}' slist


flogrr
flogr@yahoo.com

 
Flogrr,
No input break from getline.
The FS=&quot;&quot; split gives me character
(single) definitions.
Looks like this:

F
save(yes/no)
P
save(yes/no)
P
save(yes/no)
P
save(yes/no)
ends.

You get the idea.
Seems like it should be very simple.
Gawk 3.1.0 is what I am using.
I decided to write the program in the shell
and it's fine, just a little longer.
Thanks.
 
I tried it in my gawk and it did the same thing.

Trouble is that gawk sets each character to a
field if you set the FS to null. So, you get only
the first character displayed and in your output.

It now works by dumping the BEGIN and changing
the $1 to $0.

gawk '
{
print &quot;&quot;
print $0 &quot; ?&quot;
print &quot;&quot;
printf &quot;SAVE(y/n): &quot;
getline choice < &quot;-&quot;

if (choice == &quot;yes&quot;) {
print $0 >> &quot;/tmp/foo&quot;
} else {
next
}

}' slist

If this now works for you, problem solved at last!


flogrr
flogr@yahoo.com

 
Yeah, Thats the ticket.
Way simple, I'm trying to empty the red sea sometimes
with awk just because it looks like it could be done.

Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top