Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date field from file1 to file2

Status
Not open for further replies.

Orale

Technical User
Nov 8, 2006
5
0
0
US
Good morning everyone.
Let me start by saying that this forum ROCKS!!! (You actually got me started using AWK (About 2 weeks now)).

I have a question and was not able to find the answer in the forum's archive.

I have file1 that looks like this:
000001 -- TAA -- 20061024 -- MOREDATA -- MOREDATA
000002 -- TCA -- 20051015 -- MOREDATA -- MOREDATA
000003 -- TAA -- 20061107 -- MOREDATA -- MOREDATA
etc....

Then file2 looks like this:
000001 -- TAA -- MOREDATA -- MOREDATA -- MOREDATA
000002 -- TCA -- MOREDATA -- MOREDATA -- MOREDATA
000003 -- TAA -- MOREDATA -- MOREDATA -- MOREDATA
000004 -- TAA -- MOREDATA -- MOREDATA -- MOREDATA
000006 -- TCA -- MOREDATA -- MOREDATA -- MOREDATA
etc....

I generate file2 every day (runs as a cronjob) so it contains the latest updates of data and adds new records as they get created.
I found file1 and noticed that it is consistent with my file2 as far as the first record is concerned and it also contains an accurate date of when the records are created but, it doesn't get updated everyday and it might have less records than file2 (file2 also contains more fields).

What I would like to do is get the date field from file1 and place it before the first "MOREDATA" field under file2 and if a record in file2 doesn't exist in file1, to enter the current date.

OR.... is there a way that as I generate file2, place the current date either in the middle or the end of every record so as the file starts archiving, all it has to do is enter the current date on the new records? I tried doing "date" under field3 but it enters the current date on all the fields as the report gets updated the next day and I need it to keep the date of when the records get first created.

Heres how I create file2:
awk -F, '
NR==FNR{a[$1]=$0;next}
{for(i in a)if($1~i){print a" -- "$3" -- "$4" -- "$5" -- "$7" -- "$10" -- "$14 ;next}}
' somedata.txt mydata.txt >>file2

I believe that once I get file2 with the date, I won't be needing file1 since I will be archiving file2 and then can compare the new file2 against the last archived file2 and get it going but I have the need of using file1 since it contains the accurate dates for every record (about 200) and I don't want to do it manually mainly for the room for error (yeah, it will take me too long as well).

Sorry this was long but I tried to be as clear as possible.
Please let me know if anyone has an answer to my question.

Thank you.

 
It's not clear to me what the relationship between somedata.txt, mydata.txt and file1 and file2 are.

But using the example data you posted for file1 and file2 this seems to do the trick:

Code:
awk -v TODAY=`date +%Y%m%d` '
BEGIN { FS=OFS=" -- " }
NR==FNR{date[$1]=$3;next}
{
        if ($1 in date) { DATE=date[$1] } else { DATE=TODAY }
        print $1,$2,DATE,$3,$4,$5
}
' file1 file2 > file3

Even if it's not exactly what you require, I think it should point you in the right direction

Annihilannic.
 
Thank you Annihilannic!
That worked perfectly!!!!

Sorry about the mix up with somedata.txt, mydata.txt and file1 and file2.

Again, this forum ROCKS!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top