Date variable in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date variable in shell script
# 1  
Old 10-18-2010
Date variable in shell script

Hello guys n girls,

I am using ksh script on AIX platform.
I am getting a DATE from input file in "YYYYMMDDHHMISS" format.
Now I want the same date in "YYYYMMDD" format into another variable.

For example,
if the DATE is 20101018121445
then I need 20101018 in another variable.

Please let me know the command to do it.
Thanks in advance.
# 2  
Old 10-18-2010
On solaris on command line below date command gives 20101018, on AIX you can check the manual page of date cmd.
Code:
date '+%Y%m%d'

# 3  
Old 10-18-2010
Code:
$ DATE=20101018121445
$ DATE2=`echo $DATE | cut -c1-8`
$ echo $DATE2
20101018

This User Gave Thanks to cabrao For This Post:
# 4  
Old 10-18-2010
Something like this,

Code:
new_dt=$(echo "20101018121445" | cut -c1-8);echo $new_dt

# 5  
Old 10-18-2010
Code:
DATE=`date +%Y%m%d%H%M`
NEW_DATE=`echo ${DATE:0:8}`

# 6  
Old 10-18-2010
Quote:
Originally Posted by Poonamol
I am getting a DATE from input file in "YYYYMMDDHHMISS" format.
Now I want the same date in "YYYYMMDD" format into another variablee.

For example,
if the DATE is 20101018121445
then I need 20101018 in another variable.

This will work in any POSIX shell:
Code:
date=20101018121445
yyyymmdd=${date%??????} ## remove the last six characters

In bash and ksh93 you can do:
Code:
yyyymmdd=${date:0:8}

# 7  
Old 10-19-2010
Thank you so much for your time and reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. Shell Programming and Scripting

Tar with variable date in separate shell

Hi, I am trying to Zip a folder inside a shell script like below. It successfully taring but it only returns Null inside the variables. Searched a lot but no clue to finding the root cause. testno=1; date=20171128; DIR=/root/ sh -c 'cd $DIR; tar cvf "AWS.${testno.${date}.tar" "./AWS"' ... (5 Replies)
Discussion started by: pradyumnajpn10
5 Replies

4. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

5. UNIX for Dummies Questions & Answers

How to write a shell script to Run it the from Date A to Date B?

Hi , How would i write a shell script to run the code from one date to another date EXample 2014-01-01 to 2014-02-28, can i any provide some clue on this (4 Replies)
Discussion started by: vikatakavi
4 Replies

6. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

7. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

10. Shell Programming and Scripting

Specify a previous date as start date in shell script

Hi, I am trying to modify a script which accepts date in format dd/mm/yy. I am trying to modify the script so that it retrieves the date that was 15 days earlier from today as start date. Eg.if today is 05/09/2006, the script should retrieve 21/08/2006 as start date. Is there any script/code to... (2 Replies)
Discussion started by: ritzwan0
2 Replies
Login or Register to Ask a Question