Date with Wildcards in Command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date with Wildcards in Command
# 1  
Old 12-28-2011
Date with Wildcards in Command

I have the following script (parts from help on this forum, thanks y'all):
Code:
#!/usr/bin/ksh

date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
        0)
           MONTH=`expr "$MONTH" - 1`
                case "$MONTH" in
                        0)
                           MONTH=12
                           YEAR=`expr "$YEAR" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
 
for arg in  16 1 2 3 17 21 25 31 34 41 44 102 127

do

nawk -F, -v arg=$arg '{if($10==arg && $12~/CA[0-5]$/){SUM+=$13}}END{print(d"\t"arg"\t"SUM) >> "/tmp/Heather/DailyPegs.txt"}' d="$(da
te +%Y%m%d | awk '{printf"%8d\n",($1-1)}')" /opt/PlexDC/statsdir/PegCount/secondary/*$YEAR$MONTH$DAY* | sort -nr -k 2
 
done

nawk -F, '{if($2==$5){a[$2]++}}END{for(i in a){print(d"\t"i"\t"a[i]) }}' d="$(date +%Y%m%d | awk '{printf"%8d\n",($1-1)}')" /opt/Ple
xDC/statsdir/CDR/secondary/*$YEAR$MONTH$DAY* | sort -nr -k 2


The part that isn't working is the 2nd NAWK statement, particularly, where I have indicated "*$YEAR$MONTH$DAY".

I have the date set this way because I have several servers that will be receiving these queries, and the file names in each server starts with the server name, so I want to wildcard out the file name except for the same date on each server.

When I comment out this nawk statement, and just do an
Code:
#echo ls -l /opt/PlexDC/statsdir/CDR/secondary/*$YEAR$MONTH$DAY*

I get every file in the directory, not just files from yesterday.
Smilie

Last edited by Franklin52; 12-29-2011 at 10:39 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-28-2011
1. Please indent your code. Use code tags where necessary.
2. Looks like your initial steps to find the previous days' date is not working.
3. If you have bash try this: date -d"1 day ago" '+%m %d %Y'. Not sure if this'll work on ksh.
4. Or another work around to find yesterday's date is:
Code:
x=$(echo "`date +%s` - 86400" | bc)
date -d @$x '+%m %d %Y'

5. Again, this part in your awk is not fool-proof: d="$(date +%Y%m%d | awk '{printf"%8d\n",($1-1)}'. If date were to fetch "20111201", variable 'd' would hold "20111200" which isn't a valid date now, is it?
6. The reason you're getting all files is because, $YEAR$MONTH$DAY may not be holding any values. So, shell sees it as ** and not *$YEAR$MONTH$DAY*

Last edited by balajesuri; 12-28-2011 at 08:42 PM..
# 3  
Old 12-29-2011
The first part of the script works good. It does indeed calculate the date for yesterday, and pulls the items I need.

I only have the problem on the second nawk command. Even when i just try to do

Code:
"echo ls -s *$YEAR$MONTH$DAY*

it gives me every file in the directory, not just the oens with yesterday's date.

Since the $YEAR$MONTH$DAY works in the first nawk cmmand i use (I confirmed with manual compilation) I cannot figure out why on the second one it is processing all of the files in the directory, not just the ones with yesterday's date.

What is a "code tag"?

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by methyl; 12-29-2011 at 02:16 PM..
# 4  
Old 12-29-2011
You refer to the second "nawk" command. It it my eyes or does the first command say "nawk" and the second command say "awk" ???
I'll re-phrase that. Both the lines which start "nawk" have an "awk" further up the pipeline.


There is a left curly bracket { near the top of the script , but no matching right curly bracket }. Should it be between the last "esac" and the "for" ?

The purpose of the "awk" eludes me. It looks like the code from your previous thread which incorrectly attempts to calculate yesterday's date.

Last edited by methyl; 12-29-2011 at 02:50 PM..
# 5  
Old 12-29-2011
I have 2 lines that each start with "nawk". Agreed, each of those lines also have "awk" piped in the line, but I am referring to the the 2nd line that starts out with "nawk".
# 6  
Old 12-29-2011
Any comment about the curly brackets? It could affect the structure of the script.

What Operating System and version do you have? On Solaris "awk" and "nawk" behave quite differently.

Some Operating Systems have a finite limit to the length of a command.
I wonder about the maximum number of files you expect when expanding:
Code:
/opt/PlexDC/statsdir/PegCount/secondary/*$YEAR$MONTH$DAY*
/opt/PlexDC/statsdir/CDR/secondary/*$YEAR$MONTH$DAY*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

3. UNIX for Dummies Questions & Answers

Find command and use of wildcards

greetings, below is the find command i am using for some filesystem maintenance: find /data/Engine \( -type d -name .snapshot -prune -o -type d -wholename "/data/Engine/*/CAE" \ -prune -o -type d -wholename "/data/Engine/*/CAD" -prune -o -name ".*.case" \)\ -mtime +365 -print0 -fls... (5 Replies)
Discussion started by: crimso
5 Replies

4. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

5. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

6. UNIX for Dummies Questions & Answers

Help with rm command with wildcards

Hello everyone. My first time posting here. I have a question that may seem very insignificant to some but is one that I've been trying to address for the past several days (haven't had any luck looking online). I'm trying to clean a directory by removing old files that we no longer need.... (2 Replies)
Discussion started by: galileo1
2 Replies

7. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. Shell Programming and Scripting

using wildcards in this perl command

Hi there, is it possible to use wild cards in this statement ssh $remote_server 'perl -pi -e "s,EXP_SERIAL_19b8be67=\"\",EXP_SERIAL_`hostid`=\"UNKNOWN\"," /var/myfile' This command works fine but the bit in bold (the 8 character hostid) will not always be 19b8be67 so I was hoping I could... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

10. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies
Login or Register to Ask a Question