Gawk to take user input when a file is already given

  • Follow


Hi,
I can see a close file command in awk but I can't open it again.
Actually I want to take user input in gawk script which can't get
command line input as it automatically calls gawk when executed. The
command line input goes to gawk instead of my script and gawk tries to
open whatever is written at command prompt. Is there a way to make
getline read from stdin instead of the filename given at command
prompt and then open that file afterwards?
Thanks,
Asra
0
Reply asra_baig 10/11/2003 1:27:54 AM

On 10 Oct 2003 18:27:54 -0700, asra_baig@rocketmail.com (Asra) wrote:

>Hi,
>I can see a close file command in awk but I can't open it again.
>Actually I want to take user input in gawk script which can't get
>command line input as it automatically calls gawk when executed. The
>command line input goes to gawk instead of my script and gawk tries to
>open whatever is written at command prompt. Is there a way to make
>getline read from stdin instead of the filename given at command
>prompt and then open that file afterwards?
>Thanks,

Something like this:

  BEGIN{
	while( ( getline < "/dev/stdin" ) > 0 ) {
		work on STDIN here
	}
  }
  {
	work on file here
  }

The question doesn't agree with the subject, so to read STDIN during
the file:

  {
	condition test that activates STDIN {
		while( ( getline < "/dev/stdin" ) > 0 ) {
			work on STDIN here
		}
	}
	else {
		code to work on file
	}
  }


T.E.D. (tdavis@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)
0
Reply Ted 10/11/2003 2:26:03 AM


In article <bc519543.0310101727.5a0c93b7@posting.google.com>,
Asra <asra_baig@rocketmail.com> wrote:

% command line input as it automatically calls gawk when executed. The
% command line input goes to gawk instead of my script and gawk tries to
% open whatever is written at command prompt.

To come at your question from a different angle, it's possible to
pre-process the command line and strip out the bits that aren't
files. The simplest way is to have the calling script assign
variables, in the form

  awk 'script' var="value" var2="value2" files

or

  awk -v var="value" -v var2="value2" 'script' files

Apart from that, the command-line can be manipulated in a BEGIN block.
It's tokenised and stuck into an array called ARGV[], and it is this
array that's used to determine what files to process in the
pattern/action part of the script.

  BEGIN {
     var = ARGV[1]
     ARGV[1] = ""
     var2 = ARGV[2]
     ARGV[2] = ""
  }

run as

  awk 'script' "value" "value2" files

, the program will assign var and var2 appropriately, then start
processing the necessary files.
-- 

Patrick TJ McPhee
East York  Canada
ptjm@interlog.com
0
Reply ptjm 10/11/2003 7:15:20 PM

On 10 Oct 2003 18:27:54 -0700, Asra 
  <asra_baig@rocketmail.com> wrote:
> Hi,
> I can see a close file command in awk but I can't open it again.
> Actually I want to take user input in gawk script which can't get
> command line input as it automatically calls gawk when executed. The
> command line input goes to gawk instead of my script and gawk tries to
> open whatever is written at command prompt. Is there a way to make
> getline read from stdin instead of the filename given at command
> prompt and then open that file afterwards?
> Thanks,
> Asra

Use "-".


-- 
Cheops' Law:
	Nothing ever gets built on schedule or within budget.
0
Reply Bill 10/14/2003 5:27:33 AM

3 Replies
199 Views

(page loaded in 0.067 seconds)

Similiar Articles:













7/17/2012 4:03:37 AM


Reply: