Need help with "date" in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with "date" in ksh script
# 1  
Old 12-12-2016
Need help with "date" in ksh script

The script below works just fine.
Now i want to instead of just printing the last 7 days i want to print all the days last month. So if the script runs now, it will show all the 30 days in november.
I dont want any days to be shown in this month....
Im stuck, dont know how i will gett this to work?
******************************************
Code:
#!/bin/ksh

TEMP=/file/home/test.txt
trap "rm -f $TEMP" SIGHUP SIGINT EXIT

z=0
for i in 1 2 3 4 5 6 7 ; do

for w in `printf '%(%Y-%m-%d)T\n' ''$i' days ago'`; do XXX=$(grep $w /honey/logs/statistics/s* | wc -l ) ; done
echo "S" $w " :"$XXX >> $TEMP
z=$((z+$XXX))
done
echo "" >> $TEMP
echo "S total: $z" >> $TEMP
echo "" >> $TEMP


mailx -s "TEST" test@test.com < $TEMP
exit

---------- Post updated at 09:55 PM ---------- Previous update was at 09:39 PM ----------

I forgot to mention that /honey/logs/statistics/s* files
looks like this:
Code:
s_201612
s_201611
s_201610

And for example the s_201612 looks like inside:
Code:
2016-12-08 23:46:29:765          S  ERROR
2016-12-08 23:59:07:468          S  ERROR
2016-12-09 00:00:42:008          S  SUCCESS
2016-12-09 00:01:09:097          S  ERROR

and the script output (mail output) right now something like this:
Code:
S 2016-12-11  :100
S 2016-12-10  :200
S 2016-12-09  :100
S 2016-12-08  :300
S 2016-12-07  :500
S 2016-12-06  :100
S 2016-12-05  :400

S total: 1700


Last edited by vgersh99; 12-12-2016 at 05:38 PM.. Reason: code tags, please!
# 2  
Old 12-12-2016
Maybe something like:
Code:
wc -l < "$(printf '%(s_%Y%m)T' '1 month ago')"

# 3  
Old 12-13-2016
Quote:
Originally Posted by Don Cragun
Maybe something like:
Code:
wc -l < "$(printf '%(s_%Y%m)T' '1 month ago')"

Thanks but its not what im looking for..
# 4  
Old 12-13-2016
In what way is it not what you're looking for? Be specific.
# 5  
Old 12-13-2016
Yes, some idea what you expect might help. Howsoever, try
Code:
eval seq "$(date -d $(date +"-%ddays") +'-s" %b %n" 1 %d')"
1 Nov 
2 Nov 
3 Nov 
4 Nov 
5 Nov 
6 Nov 
7 Nov 
8 Nov 
9 Nov 
10 Nov 
11 Nov 
12 Nov 
13 Nov 
14 Nov 
15 Nov 
16 Nov 
17 Nov 
18 Nov 
19 Nov 
20 Nov 
21 Nov 
22 Nov 
23 Nov 
24 Nov 
25 Nov 
26 Nov 
27 Nov 
28 Nov 
29 Nov 
30

It has two drawbacks:
- the last month name is missing (as the seq separator string is used)
- it needs the (dangerous) eval command.

Still it might be something to start with.


EDIT: Hoppla, reading carefully and completely sometimes helps: seq allows for a floating format specifier. Try
Code:
eval seq "$(date -d $(date +"-%ddays") +'-f"%%.0f %b" 1 %d')"
1 Nov
2 Nov
.
.
.
29 Nov
30 Nov


Last edited by RudiC; 12-13-2016 at 04:32 PM..
# 6  
Old 12-14-2016
Could you not just have a case statement to define the number of days in each month or perhaps base it on the cal command? You will need a way to work out what last month is from the current date and/or prompt for input.

I must say I'm confused by the output you suggest. I cannot see how you generate it from the input. Are you trying to summarise some figures for each day that are recorded in a mixed up input file from a number of tills/stores/areas? It's difficult to tell.

Can you elaborate a bit more? It might be possible to streamline the process with an awk but you would need to be very specific with the input, calculations and the output required.



Kind regards,
Robin
# 7  
Old 12-14-2016
Quote:
Originally Posted by rbatte1
or perhaps base it on the cal command?
This was my first impulse too: why would you fumble around with some date-shenanigans when cal would provide all dates in a given month from the start?

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

How to fix this "Input is not UTF-8" while executing ksh script?

Hi, I have an automated script which have set of sqls but when i am trying to execute the automated script by using nohup command, it is throwing an error like "Input is not UTF-8" . And when i digged in to it, i am getting some "Â" character in the shell script. How to avoid this!? Thanks. (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

4. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

5. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, I´ve already searched the forum but can´t find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

6. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

7. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

8. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

9. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

10. Shell Programming and Scripting

ksh script as a login shell return "no controlling terminal"

I have created a ksh shell script and used it as a login shell for a user. </etc/passwd> lramirev:x:111:200:Luis:/export/home/menush:/usr/local/menush/menush My shell script is like this: </usr/local/menush/menush> #!/bin/ksh # if ] then . $HOME/.profile fi ... (8 Replies)
Discussion started by: lramirev
8 Replies
Login or Register to Ask a Question