Sponsored Content
Top Forums UNIX for Beginners Questions & Answers UNIX script to replace old date with current date dynamically in multiple files present in a folder Post 303034681 by Shankar455 on Thursday 2nd of May 2019 08:25:37 AM
Old 05-02-2019
UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and
I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server

Code:
$ cat temp.txt
RTG*888*TD8*20180201~
TWW*888*RD8*20180201-20180201~
RTG*888*TD8*20180201-20180201~
KCG*888*TD8*20180201-20180201~

I want the output as below by changing date. Please help. I am looking for UNIX script to make it work for all files present in that directory

Code:
RTG*888*TD8*20190417~
TWW*888*RD8*20180201-20180201~
RTG*888*TD8*20190417-20190417~
KCG*888*TD8*20180201-20180201~

Thanks in advance

Code:
for file in *; do
   # check if it's a file
   if [ ! -f "$file" ]; then
        # if not, next entry
        continue;
   fi

   # run the script
   while IFS='*' read -r str1 num str2 date; do
        if ["$str1" = "DTP"] || ["$str2" = "D8"]; then
            curdate=$(date +%Y%m%d)
            date="${curdate}-${curdate}~"
        fi
        printf "%s*%s*%s*%s\n" "$str1" "$num" "$str2" "$date"
    done < "$file"

done


Last edited by vgersh99; 05-02-2019 at 10:37 AM.. Reason: code tags, please!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write current Date dynamically in a file

Hi, I have a requirement where I need to write the current date to the file. This remaining content of this file does not change and we use this file to concat with another one. Only thng is that I need to write the date to say, record number 10 of this file everyday. Can this be done in... (5 Replies)
Discussion started by: Sree_2503
5 Replies

2. Shell Programming and Scripting

Renaming of multiple files with current date

Hi, I have a fixed 4 files in each different directory. The total 17 directories are there each one having 4 files inside it. I need rename all of them with current date. The files formates will be as below: Folder1: abc_NOR_xyz_ddmmyyyy.txt abc_NOR_ghij_ddmmyyyy.txt Folder2:... (5 Replies)
Discussion started by: rjanardhan83
5 Replies

3. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies

4. Shell Programming and Scripting

Replace date on a line with current date

Hi Guys, I have a file with following content From 20121014 : To 20121014 Number of days : 1 1234 1245 1246 1111 Everyday i run my script i want to modify "To" date on the first line with current date. I have set the current date in script as RUN_DATE=`date -u +%Y%m%d` So i want... (9 Replies)
Discussion started by: jakSun8
9 Replies

5. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

6. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

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

8. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

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

9. 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

10. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies
rdate(1M)                                                 System Administration Commands                                                 rdate(1M)

NAME
rdate - set system date from a remote host SYNOPSIS
rdate hostname DESCRIPTION
rdate sets the local date and time from the hostname given as an argument. You must have the authorization solaris.system.date on the local system. Typically, rdate is used in a startup script. The inetd daemon responds to rdate requests. To enable inetd response, the lines invoking the time command in inetd.conf must not be com- mented out. USAGE
The rdate command is IPv6-enabled. See ip6(7P). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWrcmdc | +-----------------------------+-----------------------------+ SEE ALSO
inetd(1M), inetd.conf(4), attributes(5), ip6(7P) SunOS 5.10 15 Feb 2001 rdate(1M)
All times are GMT -4. The time now is 02:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy