Re: Scripting Question: date related (454 Views)
Reply
Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 1 of 13 (666 Views)
Accepted Solution

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.

 

Honored Contributor
Patrick Wallek
Posts: 13,774
Registered: ‎06-21-2000
Message 2 of 13 (662 Views)

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.

Honored Contributor
Steven Schweda
Posts: 9,089
Registered: ‎02-23-2005
Message 3 of 13 (642 Views)

Re: Scripting Question: date related ,

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 4 of 13 (635 Views)

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.

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 5 of 13 (628 Views)

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,

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 6 of 13 (622 Views)

Re: Scripting Question: date related

[ Edited ]

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.  ;-)

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 7 of 13 (614 Views)

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.

 

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 8 of 13 (589 Views)

Re: Scripting Question: date related

>Thanks to all who responded. Kudos.

 

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

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 9 of 13 (576 Views)

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,

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 10 of 13 (561 Views)

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"

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 11 of 13 (469 Views)

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,

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 12 of 13 (465 Views)

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.

Frequent Advisor
rveri-admin
Posts: 50
Registered: ‎06-25-2012
Message 13 of 13 (454 Views)

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.

 

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.