PDA

View Full Version : GREP problems


t006sfh
03-04-03, 19:12
Hi there!!!

My problem is the following:

I am traying to made a script that should do the following:

1. Get from the system the Current month (e.g. Apr)
2. Get from the system the Current date (e.g. 02)
3. Concatenate those 2 values (e.g. Apr 02)
4. Then list the files that are in a certian directory an then grep them in order to get only the files that were updated only that date and create a file with the list of the files grepped

The script that I made is the following:

##Start##
month=`date +'"%h '`
date=`date +'%d"'`
time=$month$date

ls -lt /pusa/nchs/bdr55/data/trace/*1200*|grep $time > /tmp/text
##End##

Everytime that i run it, it failed with the following messahe:

grep: 0652-033 Cannot open 04".

when I ran the command manually, without variables its ran perfectly:

ls -lt /pusa/nchs/bdr55/data/trace/*1200*|grep "Abr 02" > /tmp/text


Could you please help on this issue?? what is wrong????

Thanks in advance,
Freddy

lelle12
03-12-03, 18:49
I'm not sure I get it, you would like to list files in a directory that is changed today? Something like:

find /pusa/nchs/bdr55/data/trace/ -name "*1200*" -ctime 0

should do the trick. Check man find for details on find

HTH
/Lennart

jwall
05-01-03, 11:35
Hi Freddy

change the grep statement to read

grep "$time"

instead of grep $time

Ruudboy
05-11-03, 11:10
Can't you use the find command, with the mtime option?

pooja
07-15-03, 12:09
Hello,

check for value u getting in $time

It seems that variable($time) is getting wrong value.Because from example, it seems u want month name for which u should use %b instead of %h to put value in $month

Pooja

mshankars
08-09-03, 07:13
hello pooja,
We can use both the commands date +%d and date +%h to get the month . The problem lies at the &time instead of '&time' .He said he can get it correctly when he wexecutes manually. But if he runs as a script he needs to give '&time' .Am I right?

chillies
08-10-03, 15:37
jwall was correct when (s)he said that the command should be grep "$time" rather than grep $time.

The original command pipeline expects grep to search standard input for the string specified in the envvar time, which it will do if it is given an unique argument. In the latter case, the shell expands $time into Abr 04 which grep interprets as two arguments separated by a space, and tries to find the string Abr in the file 04. Hence the error. The other number in the error message is an error code from the command (smells like AIX from here ...).

Putting grep '$time' into a script will not work. We want the shell to expand the envvar, wheras this will tell grep to find the literal string $time in stdin.

Alex

Originally posted by mshankars
hello pooja,
We can use both the commands date +%d and date +%h to get the month . The problem lies at the &time instead of '&time' .He said he can get it correctly when he wexecutes manually. But if he runs as a script he needs to give '&time' .Am I right?