Replace Date field in Unix File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Date field in Unix File
# 1  
Old 07-19-2010
Replace Date field in Unix File

I have a data file having first 19 characters having the date in the below format-

2010-04-29-00.00.00

I have to check this date field ( first 19 characters) against some specific dates and if date is not in 3 valid dates ( business date available to me , business date - 1 , businessdate -2 ) I have to replace with business date.

For example if business date is 2010-04-29-00.00.00

1) I need to check the date field ( first 19 characters) in the file
2) If the date is not from valid dates (2010-04-29-00.00.00 , 2010-04-28-00.00.00, 2010-04-27-00.00.00) I have to replace it with business date (2010-04-29-00.00.00)

Can somebody suggest the way to achieve this using awk or sed.
# 2  
Old 07-19-2010
You need provide the sample input file first.
# 3  
Old 07-20-2010
File ouput and problem further explained

The file looks like this-
Code:
2010-04-29-00.00.00TSCN00000171585               *         **   ADDR
2010-04-27-00.00.00TSCN00000171585               *         **   ADDR
2010-04-25-00.00.00TSCN00000171585               *         **   ADDR
2010-04-21-00.00.00TSCN00000171585               *         **   ADDR
2010-04-11-00.00.00TSCN00000171585               *         **   ADDR

The First 19 characters are the date field.

My program has got 3 possible valid dates. In the above file I want that if the date is not in

2010-04-29-00.00.00
2010-04-28-00.00.00
2010-04-27-00.00.00

it should be replaced with

2010-04-29-00.00.00


So the last 3 dates (first 19 characters in file) should be changed to
2010-04-29-00.00.00

and file should look like this

Code:
2010-04-29-00.00.00TSCN00000171585               *         **   ADDR
2010-04-27-00.00.00TSCN00000171585               *         **   ADDR
2010-04-29-00.00.00TSCN00000171585               *         **   ADDR
2010-04-29-00.00.00TSCN00000171585               *         **   ADDR
2010-04-19-00.00.00TSCN00000171585               *         **   ADDR

# 4  
Old 07-20-2010
Hi,
try this,
Code:
awk  -F'-00' '{if(/(2010-04-29|2010-04-28|2010-04-27)/)  {print $0}else {gsub($1,"2010-04-29",$0);print $0}}' infile

# 5  
Old 07-20-2010
Code:
awk  '! match($0,/^2010-04-2[7-9]-00.00.00/) {s=substr($0,20,length-19); $0="2010-04-29-00.00.00"s}1' urfile

# 6  
Old 07-21-2010
Thanks , further clarification required

Thanks for your replies. The awk command is working as standalone.

However my 3 valid dates are available in 3 variables-

var1=2010-04-29-00.00.00
var2=2010-04-28-00.00.00
var3=2010-04-27-00.00.00

I have to fit this into the command suggested by you-

Code:
awk  -F'-00' '{if(/(2010-04-29|2010-04-28|2010-04-27)/)  {print $0}else {gsub($1,"2010-04-29",$0);print $0}}'  file1

# 7  
Old 07-21-2010
Code:
var1=2010-04-29-00.00.00
var2=2010-04-28-00.00.00
var3=2010-04-27-00.00.00

awk  '! match($0,/'"$var1"'|'"$var2"'|'"$var3"'/) {s=substr($0,20,length-19); $0="2010-04-29-00.00.00"s}1' urfile

awk  -F'-00' '{if (/'"$var1"'|'"$var2"'|'"$var3"'/)  {print $0} else {gsub($1,"2010-04-29",$0);print $0}}' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. UNIX for Beginners Questions & Answers

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 $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

4. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

5. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

6. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

7. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

8. Shell Programming and Scripting

UNIX Command to replace particular field

Hello All, I need your help in getting a command. I have a input file as below: "A0C0VN","XZ70720","02","MEMBER ELIGIBILITY_DC",2012021308,"",,,20120213 I need a command to remove the field in Bold. To be specific the 5th field along with the next comma (2012021308,) needs to be taken... (6 Replies)
Discussion started by: Praveenkulkarni
6 Replies

9. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies

10. Shell Programming and Scripting

replace a similar field in a file

Hello, I am having a file where I have to replace the port values with the variable I defined. The file is an extract of an xml file: <NameValuePair> <name>Service1</name> <value>tcp:32406</value> </NameValuePair> <NameValuePair> ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question