I am completely newbee to unix.. Can someone help me with a requiement 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. i am using AIX server
$ cat temp.txt
RTG*888*TD8*20180201~
TWW*888*RD8*20180301-20180301~
RTG*888*TD8*20180401-20180501~
KCG*888*TD8*20180101-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
RTG*888*TD8*20190417~
TWW*888*RD8*20180201-20180201~
RTG*888*TD8*20190417-20190417~
KCG*888*TD8*20180201-20180201~
This is working for a single file:
awk -v "NewDate=$(TZ=GMT+100 date +%Y%m%d)" '
BEGIN {
FS = OFS = "*"
}
$1 == "RTG" && $3 == "TD8" {
gsub(/[0-9]{8}/, NewDate, $4)
}
1' file
but I want to apply it for all files present in directory.
If there are only dates in fourth column you can replace every group of 8 digits with the new date:
awk -v "NewDate=$(date -d '-15 days' +'%Y%m%d')" '
BEGIN {
FS = OFS = "*"
}
$1 == "RTG" && $3 == "TD8" {
gsub(/[0-9]{8}/, NewDate, $4)
}
1' file
Given your sample its output is as follows:
RTG*888*TD8*20190418~
TWW*888*RD8*20180301-20180301~
RTG*888*TD8*20190418-20190418~
KCG*888*TD8*20180101-20180201~
To apply it to all files in a directory, use a for loop and temporary files:
for f in directory/*; do
awk -v "NewDate=$(date -d '-15 days' +'%Y%m%d')" '
BEGIN {
FS = OFS = "*"
}
$1 == "RTG" && $3 == "TD8" {
gsub(/[0-9]{8}/, NewDate, $4)
}
1' "$f" > .tmpfile && mv .tmpfile "$f"
done