Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting Question: date related ,

SOLVED
Go to solution
Highlighted
rveri-admin
Frequent Advisor

Scripting Question: date related ,

Hi All,

 

The script was working well  From July 20th untill July 31st , but returned error after August 1st.

 

The script is matching files with the date stamp DS="date +%b%e%Y"

ls -l | grep $DS

 

But now it is not working as there is a space came in the value of $DS since August 1 2012.

 

 

Now it is returning a white space before the day, : thats the problem:

$ date +%b%e%Y
Aug 22012      ### See there is a white space before  2   #[Since the day is one digit , 2nd August]

 

- Earlier for "July 31st" it was returning like this:   [ it was GOOD as no space in between ]

$ date +%b%e%Y

Jul312012

 

 

The output should be like this:

Aug022012   ## See 02 is there,  instead of just " 2"   .

 

 

Experts your help please, how to get the output without the white space but a zero instead of the white space. 

Thanks in advance.

 

12 REPLIES
Patrick Wallek
Honored Contributor

Re: Scripting Question: date related ,

The date format you are seeing from the 'ls' command is the standard HP-UX format.  If the day is a single-digit day, then there are 2 spaces between the Month and the day.

 

I do not know if there is a way to change the date format without some other scripting to remove extraneous spaces.

Steven Schweda
Honored Contributor
Solution

Re: Scripting Question: date related ,

 
rveri-admin
Frequent Advisor

Re: Scripting Question: date related ,

All,

 

Sorry its my bad,

The filenames are having date stamp within the filename ,so $DS isl trying to match it,

 

here are the filenames :

 

 

$  ls -lrt

-rwxrwxrwx   1 oracle   dba      9338001 Jul 31 16:05 ortle.txt.Jul3120121605
-rwxrwxrwx   1 oracle   dba      50979785 Jul 31 16:23 ormpe.txt.Jul3120121623
-rwxrwxrwx   1 oracle   dba      9357787 Aug  1 16:05 ortle.txt.Aug0120121605
-rwxrwxrwx   1 oracle   dba      50912248 Aug  1 16:27 ormpe.txt.Aug0120121628

#--------------------------------------------------------------------------------------------------------------------

 

Thanks you in advance.

rveri-admin
Frequent Advisor

Re: Scripting Question: date related ,

Steven,

Thanks much , it works with sed,

 

$ date +%b%e%Y | sed -e 's/ /0/'

Aug022012

 

However the question is:  when the day will be more than 9, 

As there is no white space when the day goes more than 9, so it will work fine too I guess.

 

Thanks again,

 

 

Dennis Handly
Acclaimed Contributor

Re: Scripting Question: date related

A better fix is to look at ls.cat and look at the format:

$ dumpmsg /usr/lib/nls/msg/C/ls.cat | grep -e 11 -e 12

11 %b %2d  %Y
12 %b %2d %H:%M

 

So for the American nerd locale, you should use the following in your date string:

date +"%b %2d"

 

>I do not know if there is a way to change the date format without some other scripting to remove extraneous spaces.

 

Simply make a copy of ls.cat with the changes you want.

Consult the tag cloud:

http://h30499.www3.hp.com/t5/tag/ls.cat/tg-p

 

>The filenames are having date stamp within the filename

 

Then change to a different format:

$ date "+%b %02d"
Aug 02

(Which matches what Steven said but more complex.  ;-)

rveri-admin
Frequent Advisor

Re: Scripting Question: date related

Dennis thanks...

Patrick ,Steven thanks..

 

All sorry for confusion.

I am trying to match the file name,   not based on date and time stamp of ls -l output,

I am trying to match the last column (the file name , which is generating everyday with eachdays date-time stamp by another script that code I dont know, and the filename having the data-time stamp).

 

So I tried to match DS=`date "+%b%e%Y"   [ produces: "Jul312012   or  Aug 12012" ]  , it was found producing one white space from August, if day is 1 to 9. it worked day 20th when I first wrote the script, untill 31st July. But after Aug 1 didnot work.

 

 

I found this today after I checked more example, after posted in to the forum:

DS=`date "+%b%d%Y"    # [ producing correct:   Aug022012" ]   

 

[I should have taken : date "+%b%d%Y"  in first attemp.  Dont know how I missed it.]

 

 

 

 

Example:

$ DS=`date "+%b%d%Y"   :

 

FILES: in /dir1/

....

...


-rwxrwxrwx   1 oracle   dba      9338001 Jul 31 16:05 ortle.txt.Jul3120121605
-rwxrwxrwx   1 oracle   dba      50979785 Jul 31 16:23 ormpe.txt.Jul3120121623
-rwxrwxrwx   1 oracle   dba      9357787 Aug  1 16:05 ortle.txt.Aug0120121605
-rwxrwxrwx   1 oracle   dba      50912248 Aug  1 16:27 ormpe.txt.Aug0120121628
-rwxrwxrwx   1 oracle   dba      9457785 Aug  2 16:05 ortle.txt.Aug0220121605
-rwxrwxrwx   1 oracle   dba      52912249 Aug  2 16:27 ormpe.txt.Aug0220121628  

 

I want to match only last two files of  current day (Aug 02, today)

# ls -l /dir1 | grep $DS

 

-rwxrwxrwx   1 oracle   dba      9457785 Aug  2 16:05 ortle.txt.Aug0220121605
-rwxrwxrwx   1 oracle   dba      52912249 Aug  2 16:27 ormpe.txt.Aug0220121628 

#----

 

 

 

 

 

Here is the difference:

 

-------------------
# date "+%b%e%Y"
Aug 22012

 

# date "+%b%d%Y"   #[OK]
Aug022012
------------------

 

 

It works ok with the 2nd one, i.e  # date "+%b%d%Y" .

 

Thanks to all who responded. Kudos.

 

Dennis Handly
Acclaimed Contributor

Re: Scripting Question: date related

>Thanks to all who responded. Kudos.

 

Besides Kudos, you might want to mark one of them as the solution.

rveri-admin
Frequent Advisor

Re: Scripting Question: date related

Dennis,

 

I am confused about the below syntax you have mentioned, it is producing white space,

my objective to eliminate white space, as grep was resulting error,

 

>>

Then change to a different format:

$ date "+%b %02d"

 

$ date "+%b %02d"
Aug 03

 

 

Thanks,

Dennis Handly
Acclaimed Contributor

Re: Scripting Question: date related

>I am confused about the below syntax you have mentioned, it is producing white space

 

Sorry, that was when I thought you wanted the ll(1) output to have two digits and no extra space.

So you want what Steven had: date "+%b%d"

rveri-admin
Frequent Advisor

Re: Scripting Question: date related

 

Dennis,

 

Thats correct,  I missed to check the attachment properly , after I checked the sed portion of Stenvens reply,.

And Indeed this is the solution. 

 

----------------------------------------

Steven Schweda

 

alp$ date +%b%d%Y
Aug022012

 

Worked well...

-----------------------------------------

 

Steven, Thank you...

Q: How do change the green marked "solution TAB" to another reply . I am not sure if it is possible.

 

Thanks all,

Dennis Handly
Acclaimed Contributor

Re: Scripting Question: date related

>Q: How do change the green marked "solution TAB" to another reply.

 

Under the current solution's select Options > Not the Solution.

rveri-admin
Frequent Advisor

Re: Scripting Question: date related

Dennis,

Great it worked ,I am able to change the solution Mark to accepted solution.  thank you..again for all the replies and postings & quick helps.