Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.

Passing Day in string

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Passing Day in string

I need to pass days in past 3, 4, 5 day in strings.

Today is Feb 7, so past 3, 4, 5 days are:

day1="Feb 2"
day2="Feb 3"
day3="Feb 4"

I have set day1 day2 day3 as the variables. So, what I need is to have the day stings to be past 3, 4, 5 days every day.

If it Feb 8, they need to be:
day1="Feb 3"
day2="Feb 4"
day3="Feb 5"

If it Feb 9, they need to be:
day1="Feb 4"
day2="Feb 5"
day3="Feb 6"

and so on.

The format of string needs to "Feb 3" (Uppercase for the first letter, Jan, Feb, Mar, etc; space ; numeric for the day of the month).

Thank you so much for your help!
# 2  
Please provide information on your attempts to resolve.

Until so, we will refrain from sharing any guidance.

The purpose of this Board is the assist users in solving their problems. We are not a coding service. Further, we are not a homework service - and sometimes posts appear to be an attempt to have someone solve a school assignment.

Finally, we urge all members of the Forum to NOT post a solution to this question until effort to resolve is demonstrated.
# 3  
This is what I have so far, and I am kind of doing manual coding every day for day1, day2, day3
But, I like to automate it so that I can eliminate manual coding. Thank you!

exec 1> $print_cleanup_exec
day1="Feb 2"
day2="Feb 3"
day3="Feb 4"

echo "-------------------------------------------------------" > ${print_cleanup}
echo "Header to print cleanup file :   \n"  >> ${print_cleanup}
echo "         Date : `date`  \n" >> ${print_cleanup}
echo " " >> ${print_cleanup}
prior=`lpstat -o |grep -v bytes |wc -l`
cleaned=`lpstat -o |grep -v bytes |sort -nkb1 |grep -v "${day1}" |grep -v "${day2}" |grep -v "${day3}" |wc -l `
....... snipped .........

# 4  
Welcome to the forum.

Please get accustomed to providing system details like OS, shell, tools' versions, so people can taylor their proposals.

Do you have a date version that offers the -d (--date=STRING) ?
$ date +"%b %_d" -d"2 day ago"
Feb  5

# 5  
Thank you, RudiC!

root:root> uname -a
HP-UX B.11.31 U ia64 0123365846 unlimited-user license

It is HP-UX, so it is kind of tough.. I am getting this error

root:> date +"%b %_d" -d "2 day ago"
date: bad format character - _

# 6  
check LOCALE value for cal format, try:

date=$(date +"%d %m %Y")
#date="8 2 2019"

echo $date | read tday x


echo "$date" | awk '
   m=$2; y=$3;
   if ($1 <= mpd) {
      mm=m; yy=y;
      if (mm-1 < 1) { mm=12; yy=y-1; } else { mm--; }
      print "cal ", mm, yy
   print "cal ", m, y
' mpd=$max_past_day | sh | awk '
length($1) > 3 {month=substr($1, 1, 3)}
$1+=0 == $1  {for (i=1; i<=NF; i++) {days[c++]=month FS $i; daysa[d++]=$i;}}
   sc=split(past_days, pdays, ",");
   for (j=1; j<=sc; j++) ppdays[pdays[j]]=pdays[j];
   for (i=c-1; i>=0; i--) {
      if (daysa[i]==tday) ptr=1;
      if (ptr && (prtc++ in ppdays)) out[outc++]=days[i];
   for (i=outc-1; i>=0; i--) print "day" (++k) "=\"" out[i] "\"";
' tday=$tday past_days="$past_days"

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Everything my dear colleague RudiC said applies, but also, just to show you that posting your stuff is a rewarding activity:

Originally Posted by danielshell
prior=`lpstat -o |grep -v bytes |wc -l`

Under NO circumstances you should use backticks any more! If you are not the rare exception who uses an original Bourne shell from the time computers ran on steam instead of electrical power you have a shell that can (and should) use "POSIX style" subshells:

prior=$(lpstat -o |grep -v bytes |wc -l)

Originally Posted by danielshell
cleaned=`lpstat -o |grep -v bytes |sort -nkb1 |grep -v "${day1}" |grep -v "${day2}" |grep -v "${day3}" |wc -l `

The same applies here but in addition the grep can be straightened. The sort is superfluous because you want to know the number of lines at the end which is independent of the sorting:

cleaned=$( lpstat -o | grep -ve bytes -ve "${day1}" -ve "${day2}" -ve "${day3}" | wc -l )

I hope this helps.

This User Gave Thanks to bakunin For This Post:
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
passing letters from an array into a string for string comparison
attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)...... Homework & Coursework Questions
Homework & Coursework Questions
Reading a string and passing passing arguments to a while loop
I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each...... Shell Programming and Scripting
Shell Programming and Scripting
Day of the week from a string
Hi All, I need to know how to derive the day of the week by passing the value in following format: Feb 28 2010 The output I'm expecting is Sunday or Sun. I know, I can use the following code to get the day of the week. date +%a But I want to pass the value as a string. Please help...... UNIX for Dummies Questions & Answers
UNIX for Dummies Questions & Answers
Passing string variables
HI all, Very new to shell programming and just wanted some help on how to solve the following problem. I have a small shell script which searches a given file and extracts some string parameters. I want to now be able to call this script from another shell script and somehow pass the parameters...... Shell Programming and Scripting
Shell Programming and Scripting
Passing string from function with '*'
Hi I have a shell function which returns string(ksh). The string is an sql statement. This statement can have '*' in its content (i.e. select 100 / 2 *100 from dual). When this happens ret_str will have contents of current directry I run the script from build in sql. Is there any way to fix it...... Shell Programming and Scripting
Shell Programming and Scripting

Featured Tech Videos