On Mon May 18 1998, John P. Donaldson wrote:

> This is probably a stupid question but I'm relatively new to this.  I'm
> trying to grep the current date from file1 and redirect the output to
> file2.  Right now I'm entering the current date as an argument on the
> command line.  My macro file looks like this:
> 
> grep $1 file1 > file2
> 
> I would like to be able to run the macro without having to give it the
> current date as an argument, I just don't know how to get grep to
> understand the date command.  This is how I would like it to look:
> 
> grep date +"%b/%Y" file1 > file2

Close, but no cigar...

> How do I enclose the [date +"%b/%Y"] command inside the grep command.  I
> tried enclosing the date command within ",', {, and [ and none seemed to
> work.

grep `date` file1 > file2

or alternatively (with bash):

grep $(date) file1 > file2

> I'm trying to do the same thing with a sed command by changing all dates
> in a file to the current date. Right now my command looks like:
> 
> sed s/'[0123][0123456789]\/.*\/199[78]'/'18\/May\/1998'/g $1 > $2

Replace every occurance in a line (/g) where there is any string like
"[0-3][0-9]/.*199[78]" with the current date?  No, the quoting is all
wrong... try something like this:

sed -e "s/[0-3][0-9].*199[78]/`date`/g" file1 > file2

It's ugly, but it works.  Warning: it won't change the day-of-the-week
string that belongs in the date command (but if you use something like "+%b
%Y" then you can ignore this part).

To do this "right", you'll need to use alternative pattern matching using
the pat1|pat2 construct, and/or pattern substitution in the replacement
string using the \(pat\)/\1 construct.

Heh, it's all getting a big complicated and beyond the scope of a simple
mailing list... you should read the man pages for a few utilities that use
regular expressions (ed, sed, awk, (e)grep, etc) so that you can get a
handle on how all this works.

> I'd like to replace the '18\/May\/1998' with the date command.

`date '+%d %b %Y'`

Use backquotes to enclose the command whose output you are wanting to use
as a substitution within another command.

Cheers
Tony                           .
    [EMAIL PROTECTED] _--_|\        [EMAIL PROTECTED]
    UNIX Systems Officer  /     *\   [EMAIL PROTECTED]
    Faculty of Science    \_.--._/ [EMAIL PROTECTED]
    Uni of Southern Queensland  v         Toowoomba   Australia
   -=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-=*#*=-

Reply via email to