awk's getline < "-" seems not work for pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk's getline < "-" seems not work for pipe
# 8  
Old 04-08-2009
Quote:
Originally Posted by radoulov
Did you try:

Code:
command | getline

See vidyadhar85's post above.
hey man, you seemed not to get what I was trying to do. So after I analyze the data, I need to prompt to users what to do next. So in my awk script I use getline < "-" (or getline "/dev/stdin"). It works fine for file processing but fails for pipe.

The problem can be triggered easily, e.g.

gawk ' BEGIN {
printf "Enter your name:"
getline name < "-"
}
{print name} ' somefiles
works just fine.

But it fails when

cat somefiles | gawk ' BEGIN {
printf "Enter your name:"
getline name < "-"
}
{print name} '


So my question is how to make it work for pipe (putting in BEGIN block won't work either)? Of course I can make this user interaction part in shell script, but it will make my awk script more awkward, so I want do it in awk script.

BTW I use gawk 3.1.6 and try solaris nawk, they both fails for pipe

Last edited by qiulang; 04-08-2009 at 09:04 AM..
# 9  
Old 04-08-2009
It's clear now,
you need another fd attached to STDIN.
Try this:

Code:
exec 3>&0
cat file ... file_n | 
  awk 'BEGIN {
                printf "Enter your name:"
                getline name < "/dev/fd/3"
                }
              { print name }
            '

P.S. The usage of cat in your example is only for the demonstration, isn't it?

Last edited by radoulov; 04-08-2009 at 10:04 AM..
# 10  
Old 04-08-2009
/dev/fd/0 is the alias for /dev/stdin and no I don't mean to open several files in a row(of course the usage of cat in my example is just for demonstation). I just want to read the users' input and because normally I will run my script like "some command | gawk -f myscript" , I really need to figure out how to make getline < "-" works for pipe.
So your reply won't work.
# 11  
Old 04-08-2009
I don't understand, could you give an example of the input and the desired output?
I though you wanted the following:

This is the output of the ps command:

Code:
% ps
   PID TTY      TIME CMD
  4338 pts/5    0:00 ksh
  4346 pts/5    0:00 zsh

With the code I posted you get the following output:

Code:
% exec 3>&0
% ps |
pipe>   nawk 'BEGIN {
pipe quote>                 printf "Enter your name:"
pipe quote>                 getline name < "/dev/fd/3"
pipe quote>                 }
pipe quote>               { print name, $0 }
pipe quote>             '
Enter your name:radoulov
radoulov    PID TTY      TIME CMD
radoulov   4366 pts/5    0:00 zsh
radoulov   4338 pts/5    0:00 ksh
radoulov   4346 pts/5    0:00 zsh

# 12  
Old 04-08-2009
"exec 3>&0" seems to only work when typing at shell prompt

Well, typing "exec 3>&0" at shell prompt works as you said. But if you put it in shell script, it just doesn't work (I hit this problem before and never figure out why). So the codes as you suggest would be like,

#!/usr/bin/sh
exec 3>&0
awk 'BEGIN {printf "Enter your name:" ; getline name < "/dev/fd/3"}{ print name, $1 }' $1


BUT it does not work for pipe either!! Smilie

And as I said before I don't want to wrap my awk script inside a shell script (otherwise letting the shell script to ask for users' input would be much easier), so the awk script would be like following,

#!/usr/bin/gawk -f
BEGIN {
system("exec 3>&0")
printf "Enter your name:"
getline name < "/dev/fd/3"
}
{ print name, $3 }


And it even generates the error message like "fatal: can't stat fd 3 (Bad file number)"

Last edited by qiulang; 04-08-2009 at 11:32 PM..
# 13  
Old 04-09-2009
Another option:

Code:
echo 1 2 3 | awk 'BEGIN { printf "Enter your name: "
                         getline name < "/dev/tty" }
                        { print name, $0}'

# 14  
Old 04-09-2009
Quote:
Originally Posted by rubin
Another option:

Code:
echo 1 2 3 | awk 'BEGIN { printf "Enter your name: "
                         getline name < "/dev/tty" }
                        { print name, $0}'

Yes,
rubin's solution will work in the situation qiulang describes.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script does not work when written as "one-liner"

In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work... (2 Replies)
Discussion started by: Yagi Uda
2 Replies

2. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Awk to Search and Replace inside the pipe "|"

Hi, Anyone can help me on how to replace the qoutes inside the pipe | in my Text File like belows; "AAAA"|"Test "1-A""|"Test AAAA"|"This is A" "BBBB"|"Test "1-B""|"Test BBBB"|"This is B" "CCCC"|"My Test C"|"Test "CCCC""|"This is C" The output I need like belows; "AAAA"|"Test 1-A"|"Test... (12 Replies)
Discussion started by: fspalero
12 Replies

5. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

6. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

7. Shell Programming and Scripting

awk: getline NOM < "-" script does not stop

Well , i have: $ cat example.dat 1 2 3 4 5 $ cat getline3.awk function getName (NOM) { printf "Enter a filename: " getline NOM < "-" # get response return NOM } BEGIN { printf ("\nREP: %s\n",getName(N)) } {print} This example works fine: (2 Replies)
Discussion started by: Klashxx
2 Replies

8. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies
Login or Register to Ask a Question