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
# 1  
Old 04-08-2009
awk's getline < "-" seems not work for pipe

Hi all,

I have an gawk script to get user's input, So I use

getline name < "-" (or getline name < "/dev/stdin") in my script

They both work fine when my script deals with files. But it is broken for pipes.
When I try "some command | my awk script", the variable name just gets an empty value (""), it does not get the value from stdin at all.

Why is that ? And how do I fix it ?

Last edited by qiulang; 04-08-2009 at 09:01 AM..
# 2  
Old 04-08-2009
as far as i know get line deals with following six varients
Code:
getline 
sets $0, NF, FNR, and NR. 
getline var 
sets var, FNR, and NR. 
getline < file 
sets $0, and NF. 
getline var < file 
sets var. 
command | getline 
sets $0, and NF. 
command | getline var 
sets var.

no where its mentioned that it take i/p from stdin
# 3  
Old 04-08-2009
It mentions in gawk user's guide The GNU Awk User's Guide
(seach "-").

And it works for files processing. Just seems not work for pipe.
# 4  
Old 04-08-2009
Quote:
Originally Posted by qiulang
It mentions in gawk user's guide The GNU Awk User's Guide
(seach "-").

And it works for files processing. Just seems not work for pipe.
Buddy that means
if getline < "filename"
some command ' file2
now it getline read from filename
if getline < "-"
some command' file2
now it read from file2
it doesn't mean that it read from stdin
# 5  
Old 04-08-2009
Quote:
Originally Posted by vidyadhar85
Buddy that means
if getline < "filename"
some command ' file2
now it getline read from filename
if getline < "-"
some command' file2
now it read from file2
it doesn't mean that it read from stdin
Hey just take a try getline name < "-" you will know it will work for file processing. Smilie

Actually "sed & awk" had several examples of getline < "-" to read user's input from stdin.

I just want to know how to make it work for pipe.
# 6  
Old 04-08-2009
Did you try:

Code:
command | getline

See vidyadhar85's post above.
# 7  
Old 04-08-2009
Quote:
Originally Posted by qiulang
Hey just take a try getline name < "-" you will know it will work for file processing. Smilie

Actually "sed & awk" had several examples of getline < "-" to read user's input from stdin.

I just want to know how to make it work for pipe.
You're right, getline < "-" gets the input from standard input.
What you can't do is to get input from a pipe and a file simultaneously. Get the users input in the BEGIN{} part.

Regards
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