Change Date in Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Change Date in Variable
# 1  
Old 05-23-2013
Change Date in Variable

I am hoping someone can help me with this issue.

I am extracting a date from a file that is in DD/MM/YYYY format using...

Code:
expiry_date=`echo ${line}| awk -F, '{ print $4 }' | tr -d '\r'`

What I need to do is amend the value so instead of getting 30/12/2016 I end up with 12/30/2016. I have looked in the forum but can not find anything that will help with this.

I would be ever so grateful for some guidance.

Thanks

Perry
# 2  
Old 05-23-2013
What does ${line} look like?

If it's just the date: echo $line | awk -F/ '{print $2 FS $1 FS $3}'
# 3  
Old 05-23-2013
Thanks, this is the part of code...

Code:
while read line
do

vouchercode=`echo ${line} | awk -F, '{ print $2}'`
partnumber=`echo ${line}| awk -F , '{ print $3 }'`
expiry_date=`echo ${line}| awk -F, '{ print $4 }' | tr -d '\r'`

# 4  
Old 05-23-2013
That doesn't tell me what a line looks like. I presume that comes from a file. Post part of the file.

edit: It's OK, I see the date's in field 4, missed that Smilie

Code:
expiry_date=$(echo $line | awk -F, '{print $4}' | awk -F/ '{print $2 FS $1 FS $3}')


Last edited by Scott; 05-23-2013 at 01:10 PM.. Reason: edit
# 5  
Old 05-23-2013
This is the first line of the file...

Code:
1,PJGSW-YXQR55-1905,10,30/12/2016

# 6  
Old 05-23-2013
Also using awk to split fields inside a loop is an overkill.

I would suggest using IFS to set field separator to comma and read values into different variables.

Later you can use the substring feature ${string:position:length} to rearrange month and day in the date variable value:
Code:
while IFS="," read seq vouchercode partnumber expiry_date
do
        expiry_date="${expiry_date:3:2}/${expiry_date:0:2}/${expiry_date:6}"
done < inputfile

# 7  
Old 05-23-2013
Thanks for this but alas I get an error...

Code:
perry[5]: expiry_date="${expiry_date:3:2}/${expiry_date:0:2}/${expiry_date:6}": 0403-011 The specified substitution is not valid for this command.

Any suggestions on what needs amending?

Thanks

Perry
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. UNIX for Beginners Questions & Answers

How to change existing date to current date in a filename?

Suppose i have a list of files in a directory as mentioned below 1. Shankar_04152019_ny.txt 2. Gopi_shan_03122019_mi.txt 3. Siva_mourya_02242019_nd.txt .. . . . . 1000 . Jiva_surya_02282019_nd.txt query : At one shot i want to modify the above all filenames present in one path with... (4 Replies)
Discussion started by: Shankar455
4 Replies

3. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

4. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

5. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

6. Shell Programming and Scripting

LINUX ---> Add one date to a date variable

Hi All, I am trying to add one day to the busdt variable i am extracting from a file (DynamicParam.env). I am deriving the busdt as below. Is there any function which I can use to derive as below. If busdt=20120910, then the new derived variable value should be 20120911 If... (2 Replies)
Discussion started by: dsfreddie
2 Replies

7. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

8. Shell Programming and Scripting

How to check date variable according to the current date?

Hi folks, I need to write a script that should activate a process according to the current hour. The process should be activatet only if the hour is between midnight (00:00) and 07:00. How should I create the condition? Thanks in advance, Nir (2 Replies)
Discussion started by: nir_s
2 Replies

9. Shell Programming and Scripting

How Can we change the Date format from Variable

curr_date=yyyy-mm-dd 1.we want get month from the above date 2.Change the format into yyyymmdd (1 Reply)
Discussion started by: laknar
1 Replies

10. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies
Login or Register to Ask a Question