Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 Apr 2007 08:49:56 +0100
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Gary Kline <kline@tao.thought.org>
Cc:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: first of misc questions....
Message-ID:  <462F0824.5000107@infracaninophile.co.uk>
In-Reply-To: <20070425072914.GA65634@thought.org>
References:  <20070425072914.GA65634@thought.org>

next in thread | previous in thread | raw e-mail | index | archive | help
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Gary Kline wrote:
> 	Guys,
> 
> 	This is an awk-type question.  Hopefully a one-liner.  If I
> 	need to use #!/usr/bin/awk and a BEGIN/END (or whatever it is),
> 	that's okay...
> 
> 	I want to do an ls -l in a  /home/kline/<directory> and find and
> 	edit files that are dated (let's say) Apr 19 or Mar 26.  This
> 	works to print $9 the filenames.  
> 
> 	 ls -l| awk '{if ($6 == "Apr" && $7 == 19  || $6 == "Mar" && $7
> 	 == 26 ) print $9}'
> 
> 	 What's the final part to get awk to vi $9?  Or another pipe and
> 	 xargs and <what> "vi"?  Nothing simple works, so thanks for any
> 	 clues!
> 

xargs(1) is your friend.

Simply arrange for your awk script to print out the names of all the
files you have selected to edit, then pipe the result into xargs.
Like so:

ls -l| awk '{if ($6 == "Apr" && $7 == 19  || $6 == "Mar" && $7 == 26 )
print $9}' | xargs vi

This does assume that the file names you are using do not contain
spaces, quote marks, brackets or other characters of syntactical
significance to the shell.  In that case you could use something like
this:

   find . -type f \( -mtime 6 -o -mtime 29 \) -print0 | xargs -0 vi

where find's '-print0' and the '-0' flag to xargs make the commands
produce and consume respectively a null separated list of filenames.

Unfortunately with find(1) there doesn't seem to be a way of expressing
an absolute date / time -- all you can do is the time difference between
now and when you want (which defaults to 'number of days' but can be set
to use various other time units.  I can think of a couple of ways of
calculating that, but personally I'd find it cleaner to just roll the
whole thing into a small perl script which identified the files in
question and forked off an instance of vi(1) to do the editing.

	Cheers,

	Matthew

- --
Dr Matthew J Seaman MA, D.Phil.                       Flat 3
                                                      7 Priory Courtyard
PGP: http://www.infracaninophile.co.uk/pgpkey         Ramsgate
                                                      Kent, CT11 9PW, UK
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.3 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGLwgk3jDkPpsZ+VYRAxaaAJ9H4q3vD4qqBo+FijEs+PqmaR0kaQCgidpA
kXOmJIpsODutFhLIvIoJpEE=
=fNoc
-----END PGP SIGNATURE-----



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?462F0824.5000107>