Messy Messy..Making awk think a string is a list of files 
Author Message
 Messy Messy..Making awk think a string is a list of files

I'll bet there is some canonical way to do this job.  Seems like it
should be susceptable to the process shown here recently by
A. Robbins. in this post:


   Subject: Re: Extract awk vars into shell notation for printing
   Newsgroups: comp.lang.awk
   Date: 5 Dec 2000 19:41:35 +0200

Its a little different problem.  Different enough that I'm getting
pretty lost wallowing around in various trials.

Summary of Problem:

Take a string by pipe line that looks like:
    (chopped up here for posting, but really a single line)

("/home/reader/Mail/tmp/awk-work/15"\
"/home/reader/Mail/tmp/awk-work/14" \
"/home/reader/Mail/tmp/awk-work/13" \
"/home/reader/Mail/tmp/awk-work/12")  

I want to clean up and convert this single line string to
something awk sees as a list of files to process.

I don't want to use tmp files at all.

The program that does the processing of the filenames in that string is
not shown here nor is it a part of the subject matter.

There are probably cleaner ways but so far I'm trying an approach that
uses a shell/awk script and waits behind a pipeline for the incoming
string.

Attempting to feed the fields into ARGV thinking somehow awk would
then think these were file names to process.  But the little print
statement reveals it to not be working at all.

First I tried to feed the fields directly into ARGV array something
like:

 echo <STRING>  \
 |sed 's/[\"\(\)]//g' \

 |  awk 'BEGIN{
           for (i=1;i<=NF;i++) {
                   ARGV[x++] = $i
           }
   } {print ARGV[1]}NR == 1{print}'

With the string above fed to it, the output shows:
  (blank line)
  (next line chopped for posting but was a single line)
  /home/reader/Mail/tmp/awk-work/15 /home/reader/Mail/tmp/awk-work/14 \
  /home/reader/Mail/tmp/awk-work/13 /home/reader/Mail/tmp/awk-work/12

NR == 1 is still the plain string... no fields have been fed to
ARGV, a blank line is printed at that point.

Next trying to put the string into a shell VARIABLE then use split to
feed it to ARGV

cat cnvt.sh:

   #!/bin/sh
   FILES=`sed 's/[\"\(\)]//g'`

   awk 'BEGIN {
        split ("'"$FILES"'",ARGV)
            print "<AR1=<"ARGV[1]"> AR2=<"ARGV[2]">\n"

   }NR == 1{print}'

This has worked in so far as putting the fields into ARGV but still
the rest of the program doesn't scurry off to process it as a file
name as evidenced by NR == 1 still being the same string.

Output from above script:
   (chopped for posting again)  

  <AR1=</home/reader/Mail/tmp/awk-work/15> \
  <AR2=</home/reader/Mail/tmp/awk-work/14>

  ("/home/reader/Mail/tmp/awk-work/15"
  "/home/reader/Mail/tmp/awk-work/14" \
  "/home/reader/Mail/tmp/awk-work/13"\
  "/home/reader/Mail/tmp/awk-work/12")

Is this just the complete wrong way to go about this?

  FILES=`sed 's/[\"\(\)]//g'` #| awk '{for (i=1;i<=NF;i++){

  awk 'BEGIN {
        split ("'"$FILES"'",ARGV)
      print ARGV[1], ARGV[2]

  }NR == 1{print}'



Tue, 10 Jun 2003 06:51:55 GMT  
 Messy Messy..Making awk think a string is a list of files


[...]

% First I tried to feed the fields directly into ARGV array something
% like:
%
%  echo <STRING>  \
%  |sed 's/[\"\(\)]//g' \
%
%  |  awk 'BEGIN{
%            for (i=1;i<=NF;i++) {
%                    ARGV[x++] = $i
%            }
%    } {print ARGV[1]}NR == 1{print}'

Your problem here is that you don't read from stdin in the BEGIN
section. You want to do something like this:

  |  awk 'BEGIN{
        while (getline > 0)
            for (i=1;i<=NF;i++) {
                    ARGV[ARGC++] = $i
            }
       }

[...]

%    awk 'BEGIN {
%         split ("'"$FILES"'",ARGV)
%             print "<AR1=<"ARGV[1]"> AR2=<"ARGV[2]">\n"
%    
%    }NR == 1{print}'
%
% This has worked in so far as putting the fields into ARGV but still
% the rest of the program doesn't scurry off to process it as a file
% name as evidenced by NR == 1 still being the same string.

You need to set ARGC (ie ARGC = split(...) + 1) The +1 is because the
ARGV array starts being indexed at 0, so the indices run from 0 to
ARGC-1.

--

Patrick TJ McPhee
East York  Canada



Tue, 10 Jun 2003 11:52:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Untangling a messy database

2. Messy if...else statements

3. Toolbar merging = messy windows

4. Messy Clarion patches

5. messy calculator code.. please try this out

6. messy calculator code.. please try this out

7. Messy Bindings

8. making a string copy of a list

9. Making lists from strings (need more than split)

10. Replacing a string from an input file within awk script

11. I am thinking about becoming a Head Hunter

12. I am thinking of if this is possible

 

 
Powered by phpBB® Forum Software