Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #1342

Re (4): HOW2 pass args to awk? and more

From no.top.post@gmail.com
Newsgroups comp.os.linux.misc, comp.lang.awk
Subject Re (4): HOW2 pass args to awk? and more
Date 2011-06-06 10:26 +0000
Organization A noiseless patient Spider
Message-ID <isi9vr$pns$1@dont-email.me> (permalink)
References <yga1uzfrqso.fsf@gepard2.akutech-local.de>

Cross-posted to 2 groups.

Show all headers | View raw


In article <yga1uzfrqso.fsf@gepard2.akutech-local.de>, Ralf Fassel <ralfixx@gmx.de> wrote: 

> * Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID>
> | awk 'BEGIN { 
> | 	arg1 = ARGV[1]
> | 	arg2 = ARGV[2]
> | 	arg3 = ARGV[3]
> | 	print "arg1=" arg1
> | 	print "arg2=" arg2
> | 	print "arg3=" arg3
> | }
> | ' $1 $2 $3
> 
> This will only work if you don't do any work in the awk script.  
> The $1 $2 $3 in that place on the awk command line 
> will be interpreted by awk as filenames to read.

OK, that answers a previous question.

And will they be the 3 args passed to the script
 which contains the `awk job` ?
Apparently not.

> To pass values to awk, use the -v switch.
> 
>   $ cat script
>   #!/bin/sh
>   printf "In script: 1,2,3==$1,$2,$3\n"
>   awk -v arg1=$1 -v arg2=$2 'BEGIN { print arg1, arg2 }'

>   $ ./script foo bar
>   In script: 1,2,3==dog,cat,rain
>   dog cat
> 
Understandably, I get:--
./script foo bar
In script: 1,2,3==foo,bar,
foo bar
And further ----> script2 ==
#!/bin/sh
printf "In script: 1,2,3==$1,$2,$3\n"
awk -v arg1=$1 -v arg2=$2 'BEGIN { print arg1, arg2 }'

echo 'In script after exit awk, args 1,2,3==' $1,$2,$3
--> ./script2 foo bar ==
In script: 1,2,3==foo,bar,
foo bar
In script after exit awk, args 1,2,3== foo,bar,
------------------
>> This will only work if you don't do any work in the awk script.

Ed Morton wrote:-
> I assume you mean do any work in the body of the script as opposed 
> to the BEGIN section, which is not the case in the above script. 
> Tha main problem I see with the above is that it doesn't quote the 
> shell positional parameters so if they contain spaces you'll get 
> undesirable results.
> 
> >   The $1 $2 $3 in that place on the awk command line 
> >  will be interpreted by awk as filenames to read.
> 
> Only if you write a script with more than a BEGIN section and you 
> forget to delete those arguments before leaving the BEGIN section.

OMG what a crappy-convoluted finite-state-machine is awk?

> In general you're right to recommend "-v" but it's not _always_ the 
> right option, depending on how you want a couple of conditions 
> handled, so it's good to point people to the FAQ:

>	http://cfajohnson.com/shell/cus-faq-2.html#Q24

Yes and reading faq-2.html#Q24 can lead to a
nervous-breakdown.
--------
Loki Harfagr wrote:-
> Well, yes, of course I'd agree if that script was a real script  :-)

So, write YOUR 'real script' to:

list all files in ./
which contain ON THE SAME LINE, 
 the strings "Class:"   and <stringArg1>.
 
 So that: ./<ScriptName> dog
  will list all files which contain lines like:-
Class: mouse dog fish no need for ...
----------
Then extend it to find <dog and mouse>.
Then extend it to find (dog AND mouse) OR (dog and cow).
-----------
The awk/grep stuff is trivial compared to the profoundness 
of the application, as seen by the following scenario:-   

You've got some filing-cabinets and boxes with important docus.
Tomorrow you're going to a meeting, related to dog, cow, fish.
?SOMEHOW? you collect a dozen docus with are relevant, and 
put them in your <box for meetingX>.
BTW, where/how did you 'file' docus which relate to <dog AND fish> ?

At the meeting, it's decided because of an unanticipated event,
that the bussiness must be concluded, next Thursday.

When you get <back-home> are you going to refile the docus,
or leave them in the box till next Thursday?
How do you know where the <dog AND fish> docu was filed?
If you leave the docus in <box for meetingX+Thursday>, and you 
have a unexpected-event/crisis, so as to need to miss the next
meeting, how would you know after 2 months, what/where's in 
the box?

Apparently the normal [pre-computerised] method is to
search&sort.  With computerisation we can 'index'.
Let each docu KNOW 'where it is in real-space'.
This is facilitated by the lucky fact that 'real-space is perfectly
isomorphic to a dir-sub-tree'. Ie. each docu corresponds to a
file/leaf-node of the dir-sub-tree. 

So the awk/grep script must give a list of <path/files> like:
  /Ofic2/Cabnt5/Draw1/HfileDog/History
  /Concertina/Sectn/M/meds

And perhaps the 'meds' file, has a line:-
Class: dog cow fish ...
...etc. you get it ?

Now here's the real POWER.
When you move the phyical paper-docu from a location into 
<box for meetingX>, you use `mc` [which VISUALLY with hot-keys]
easily allows you to:
* create the sub-dir for the <box for meetingX>.
* move single-files or a whole container/sub-dir if files to
a new location.
NB real paper-files in real-space correspond to
 linux-dir-files in the [isomorphic to real space] dir-tree.
 
So now you just need the computer to search the corrsponding
dir-tree to locate the relevant docus. -- QED.
------------
An extra bonus comes from `mc`:-
When it show you a list of a dozen files re. <dog OR fish>,
you just scroll down to <an interesting one> and
hit <F3> to view/read inside the file, or
hit <F4> if you want to eg. make/edit same extra notes,
  re. <next Thursdays meeting or what ever>.
  
BTW you do see that, when you move the concertina-file
[which has 26 pockets] from officeA to officeD, you just
move the corresponding dir-node [via <F6>] with `mc`,
and the computer KNOWS where all the docus in the
concertina file are located in real-space.
------
A further easy extention which is useful, after you've got
the file-list for <Class: dog AND cow> is to grep in the
file-list for files which contain <keyword/s>

So ideally, you'd want to pass these various args to the script/s.
Presently I use different scripts, and it works OK.
If it's not easy/convenient, you will tend to by-pass it and your
docus will degenerate into chaos.

The missing part of the system is the portable computer:
so that when you physically move the paper-docus, you can
immediately, make the corresponding computer change,
with out moving yourself from the paper-location to
the computer-location.

== Chris Glur.

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-17 06:27 +0000
  Re: HOW2 pass args to awk? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-17 09:12 +0200
    Re: HOW2 pass args to awk? Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> - 2011-05-17 00:21 -0700
      Re: HOW2 pass args to awk? Harold Stevens <wookie@jimbo.localdomain> - 2011-05-17 09:17 -0500
      Re: HOW2 pass args to awk? Aragorn <aragorn@chatfactory.invalid> - 2011-05-17 17:36 +0200
    Re (2): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-29 15:56 +0000
      Re: Re (2): HOW2 pass args to awk? Bill Marcum <bill@lat.localnet> - 2011-05-29 13:05 -0400
        Re (3): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-30 02:22 +0000
          Re: Re (3): HOW2 pass args to awk? Aragorn <aragorn@chatfactory.invalid> - 2011-05-30 05:09 +0200
  Re: HOW2 pass args to awk? Bill Marcum <bill@lat.localnet> - 2011-05-17 07:55 -0400
    Re (2): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-29 15:56 +0000
      Re: Re (2): HOW2 pass args to awk? Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-29 17:46 +0000
        Re (3): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-30 02:22 +0000
          Re: Re (3): HOW2 pass args to awk? Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-30 09:01 +0000
          Re: Re (3): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-31 11:05 +0000
            Re: Re (3): HOW2 pass args to awk? and more Ralf Fassel <ralfixx@gmx.de> - 2011-05-31 14:35 +0200
              Re: Re (3): HOW2 pass args to awk? and more Ed Morton <mortonspam@gmail.com> - 2011-05-31 07:56 -0500
              Re: Re (3): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-31 14:14 +0000
                Re: Re (3): HOW2 pass args to awk? and more Ralf Fassel <ralfixx@gmx.de> - 2011-06-01 10:41 +0200
              Re (4): HOW2 pass args to awk? and more no.top.post@gmail.com - 2011-06-06 10:26 +0000
                Re: Re (4): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-06-06 14:01 +0000
            Re (4): HOW2 pass args to awk? and more no.top.post@gmail.com - 2011-06-06 04:58 +0000

csiph-web