Problem passing directory as argument with awk

I'm trying to figure out what's getting passed as the argument when I try to pass a directory as an argument, and I'm getting incredibly strange behavior. For example, from the command line I'm typing:

nawk -f ./test.awk ~

test.awk contains the following:

{
  directory = $NF
  print directory;
}

And I get the following output:

.dtprofile
.slickedit
cqcc_cache
search.txt
Command.pm
xcleardiff

These are only a few of the files in my home directory. Other directories return nothing at all even though they have hundreds of files in them. How is this behavior explained? I can't make heads or tails of it.

By specifying -, your telling awk to take its input from standard input. Where does that come from (or rather, how)?

Please show the entire command line you are running.

It's not a dash. It's a tilde, referring to my home directory. That is the entire command line.

Ha ha. Sorry!!! Ich bin blind!

In any case I'm surprised that returns anything (it doesn't on my system).

What are you actually trying to achieve?

For example

find ~ | awk -f ./test.awk

will show you everything.

Well, I'm actually trying to fix a ksh and an awk script that somebody else wrote. As I understand it, it's supposed to take every file name in a directory and compare times in the file name to determine which ones to parse. The problem is that it's not picking up file names correctly. Apparently it used to work, but we changed the way we're handling time and now it's broken. The part that's odd looks like the following (this is the ksh):

LOG=`nawk -f /$toolpath/getlog.awk day=$DAY hr=$HR /tmp/dir`

The logs are in /tmp/dir

Inside the getlog.awk file, the first line is as follows:

logfile = $NF

I was trying to determine what exactly was in logfile, so I made some test scripts and was promptly baffled.

As an aside, while I think about it.... there's not a single tilde in any of that!

I still can't believe this ever worked.

Consider this:

$ cat file1
4
5

$ awk 1 file1
4
5

$ pwd
/users/home/scottn

$ echo ~
/users/home/scottn

$ ls ~
file1

$ awk 1 ~
(nothing)

$ ls /tmp
(lots of files...)

$ awk 1 /tmp
(nothing)

awk takes files, or standard input as input, not directories.

Right. The tilde was just in my test script. I can put in any directory and I get equally strange results. I tried /tmp and got nothing. I tried the root directory and got 3 files. It seems erratic.

nawk -f ./test.awk /tmp/dir/*

What does * expand to?

# mkdir tmp
# mkdir tmp2
# mkdir tmp/1 tmp/2
# echo hello > tmp2/1; echo world > tmp2/2
# awk '{ print $NF }' tmp/*               
(nothing)
# awk '{ print $NF }' tmp2/*
hello
world

That will take all of the content in all of the files. I only need the file names.

In any case, there are better ways to get a list of files in a directory. I'll just use something a little cleaner.

All the same, thanks for checking it out.

What do you think? :wink:
using shell's filename generation it will expand to all the files/directories/links/etc at a given location on a file system.

Are you surprised? Would you expect anything else?

No, I wouldn't :smiley:

But passing awk a directory name in itself (like ~), or a directory which doesn't contain any files, doesn't mean anything to it.

That's all I was saying :slight_smile:

# echo hello again > tmp/3 
# ls -l tmp
total 8
drwxr-xr-x  2 scottn  staff  68  3 Mar 22:09 1
drwxr-xr-x  2 scottn  staff  68  3 Mar 22:09 2
-rw-r--r--  1 scottn  staff  12  3 Mar 22:28 3

# awk '{ print $NF }' tmp/*
again

ah, we're in violent agreement then!

Happily, yes :slight_smile:

(and sorry that perhaps my initial point was not as clear as it might have been :o)