[Solved] Replace yesterday date with today's date except from the first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Replace yesterday date with today's date except from the first line
# 1  
Old 11-30-2012
[Solved] Replace yesterday date with today's date except from the first line

Hello,

I have a file like this:

2012112920121130

12345620121130msABowwiqiq
34477420121129amABamauee
e7748420121130ehABeheheei



in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use "sed", it will replace all the occurrences and I want to leave the first line as it is.

How can I do that?

Any help is really appreciated.
# 2  
Old 11-30-2012
Code:
#! /bin/bash
while read line
do
    if [[ $line =~ "AB" ]]
    then
        line=${line/$(date -d "-1 day" +%Y%m%d)/$(date +%Y%m%d)}
        echo $line
    else
        echo $line
    fi
done < file

OR
Code:
sed "/AB/ s/$(date -d "-1 day" +%Y%m%d)/$(date +%Y%m%d)/" file


Last edited by balajesuri; 11-30-2012 at 05:51 PM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 11-30-2012
Awk Solution:-
Code:
#!/bin/bash

YESTR=$( date -d "-1 day" +%Y%m%d )
TODAY=$( date +%Y%m%d )

awk -v YS=$YESTR -v TD=$TODAY ' {
 if(NR==1) {
  print; next;
 }
 if(match($0,/AB/)>0) {
  gsub(YS,TD,$0); print;
 } else print;
} ' infile

This User Gave Thanks to Yoda For This Post:
# 4  
Old 12-03-2012
Thanks to both of you. It worked perfectly.
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. 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

3. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

4. Shell Programming and Scripting

UNIX date fuction - how to deduct days from today's date

Hi, One of my Unix scripts needs to look for files coming in on Fridays. This script runs on Mondays. $date +"%y%m%d" will give me today's date. How can I get previous Friday's date.. can I do "today's date minus 3 days" to get Friday's date? If not, then any other way?? Name of the files is... (4 Replies)
Discussion started by: juzz4fun
4 Replies

5. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

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

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. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

9. AIX

Yesterday's date without changing anything and in one cmd line ?

I have seen references in the forum about getting yesterday's date but it is either by changing something in the system (date, time zone, ...) or with more then one line of script cmds. How can I get yesterday's date without changing anything in the system and in one single command line ? (4 Replies)
Discussion started by: Browser_ice
4 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question