[lug] Something seemingly simple

Hugh Brown hugh at math.byu.edu
Tue Dec 19 17:29:25 MST 2006


Daniel Webb wrote:
> On Tue, Dec 19, 2006 at 07:30:57AM -0700, Hugh Brown wrote:
> 
>> So the line is NULL terminated and each field can have anything in it
>> including newlines?
> 
> Yes, I'm trying to store owner, group, permissions, etc, separated by spaces,
> then a Unix filename, which means null termination.  So I can know that there
> will be X variable, space-delimited fields, followed by the filename which can
> have any character except null.  The question is how to extract only the
> filename from such a line.  I still haven't figured out a way to do it with
> the standard unix tools.
> 

I looked at the awk man page and searched on the word null.

It looks like you can use the RS variable to indicate that null is the 
record separator.  It also insists on treating a newline as a field 
separator.  But it looks like it wants records to be separated by blank 
lines.

So on my little test file (null-term):

1 2 3 4 5 ./.bash_history^@

1 2 3 4 5 ./.Xauthority^@

1 2 3 4 5 ./dist-
upgrade^@

1 2 3 4 5 ./.viminfo^@

1 2 3 4 5 ./.bash_profile^@

1 2 3 4 5 ./.bashrc^@

1 2 3 4 5 ./.vmwar^@

1 2 3 4 5 ./favorites.vmls^@

1 2 3 4 5 ./.vmwar^@

1 2 3 4 5 ./preferences^@

1 2 3 4 5 ./.vimrc^@


I can run:

  awk '{ print $6 }' RS="" null-term

and get
./.bash_history
./.Xauthority
./dist-
./.viminfo
./.bash_profile
./.bashrc
./.vmwar
./favorites.vmls
./.vmwar
./preferences
./.vimrc

So, it misses the newline in dist-\nupgrade, but that's because the awk 
script was too primitive.  You can use the NF internal variable to find 
out the number of fields and create a more complex script that prints 
the 6th field up to the number of fields.

However, when was the last time you came across a unix box that had awk 
but not perl?

Hugh



More information about the LUG mailing list