AWK : read unix command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK : read unix command
# 1  
Old 02-07-2005
AWK : read unix command

Hi,
Is there a way to have something working like the read Unix command to stop a awk ask a question and get the result.
I've try system("read a") but it does not work.
Thanks.
Franck.
# 2  
Old 02-07-2005
how 'bout [among others] - using "nawk":

Code:
{
   printf "Enter foo-> "; getline foo < "-";
   printf("line->[%s] foo->[%s]\n", $0, foo);
}


Last edited by vgersh99; 02-07-2005 at 11:01 AM..
# 3  
Old 02-07-2005
yes, but ...

I've try the following (as an exemple):
awk ' BEGIN {
printf "Enter response"
getline name < "-"
print name
}
{ a++ } ' /etc/hosts

But the script does not stop to ask my answer ...
Do I make something wrong ?
Thanks :-)
# 4  
Old 02-07-2005
as I said - use nawk with the above code.

The other way is:
Code:
{
   printf "Enter your name: "
   system("read a; echo $a") | getline name
   print name
}

# 5  
Old 02-07-2005
Thanks for your interest in my problem :-)
However, it's still not working on my AIX machine ... grrrr
awk '
{
print "-> "$0
printf "Enter response :"
system("read a; echo $a") | getline var1
print "-- " var1 "$0 : " $0

} ' ./essai.lst
where essai.lst contains only A B C D letters (one by line).
I receive the following result:
[lou:root:/home/root:] ./essai.sh
-> A
Enter response :A
A
sh: 0: not found
-- $0 : A
-> B
Enter response :B
B
-- $0 : B
-> C

Strange ...
# 6  
Old 02-07-2005
sorry about that.
If you do have nawk - I would use the original solution for nawk.

awk '
BEGIN {
cmd="read a; echo $a"
}
{
print "-> "$0
printf "Enter response :"
cmd | getline var1
close(cmd)
print "-- " var1 "$0 : " $0

} ' ./essai.lst
# 7  
Old 02-08-2005
Thanks a lot for your help, it's working fine and help me a lot.
Franck.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

2. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

3. Shell Programming and Scripting

[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this: Generated geometry (...some special descriptor) 1 0.56784 1.45783 -0.87965 8 1.29873 -0.8767 1.098789 ... ... ... ... Generated geometry (....come special descriptor) ... .... ... ... ... ... ... ... and... (4 Replies)
Discussion started by: jaldo0805
4 Replies

4. Shell Programming and Scripting

Help me for using the awk command in UNIX

Hi, Can you please help me to find/display out last Friday's date of the month using awk command in Unix/Linux. i have found this command cal 03 2013 | awk 'NF>5{last = $6} END{print last}' it show the output in single digit date like :29 But i need its output will shown in Fri Mar... (20 Replies)
Discussion started by: avig
20 Replies

5. Shell Programming and Scripting

Help with UNIX READ command usage..

Tested on : bash Will be implementing on : ksh I dont know if this is weird , or my idea of Unix stdin or stdout is completely messed up , but if I use the following command, I am getting a proper output. ls -l | head -1 | while read a ; do echo $a ;done and the output is soemthing like... (5 Replies)
Discussion started by: kumarjt
5 Replies

6. Shell Programming and Scripting

AWK getline command to read more then two lines

Hello, Am trying to print three lines in a single line using AWK getline command. Below is the command am trying and its displaying only two lines in a single line. Command: awk '{getline n; printf($0,t);next}' Can you please help me ? Thanks, Prince (1 Reply)
Discussion started by: prince1987
1 Replies

7. UNIX for Dummies Questions & Answers

Read all files in a directory for a unix command

Hello, Below, I have a unix command, which can be executable for single file. cat input.txt | sort -k3,3 > output.txt I have 100 input files in a directory. It is hectic and time taking to run the above command for all the 100 files for 100 times. Now, I want to execute the above unix... (2 Replies)
Discussion started by: koneru_18
2 Replies

8. Shell Programming and Scripting

Unix, awk to read .ksh feed

I need some advice, I have live feed containing xml messages which means there is new messages every minute. I need a script that will run every 2 hours using the current time minus 2 hours ( which I able to do) However I have problem with the date formatting i.e. One date is... (3 Replies)
Discussion started by: INHF
3 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. UNIX for Dummies Questions & Answers

need help on how to read command in unix

can any one please help me how to use "read" command for openig a file.i want to process the file record by record and then needs to search for a particular string at a particular position.can any one please let me know the code.i am new to the shell. below are sample sample records ... (3 Replies)
Discussion started by: raoscb
3 Replies
Login or Register to Ask a Question