Replacing data of output file with input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing data of output file with input
# 1  
Old 01-28-2009
Replacing data of output file with input

Hi,

I have a ksh which peocess and get me data from 3 days...
ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out
Now time.out cannot have deplicate data so what i want is when ever the appeneding is done i want look for the date in the file if it matches with the date the data is holding then i want it to be replace with new data

the time.out is like this
Code:
.
.
20090124,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938, 
20090125,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938, 
20090126,00:02:33,00:00:35,8187,00:01:42,00:01:32,02:02:08,321055,

while in the script we have the current data for which the process is running save in CUR_MAINT_DATE..
the way the output file is written is
Code:
 
Read.ksh `basename ${0}` $1 $2 ${CUR_MAINT_DATE} > runtime.out
echo >> time.out; tr -d '\n' < runtime.out >> time.out

The the script runs it initially get data of 25 ... now i want the script to cross check with the time.out file if the data is already present for that date then it have to replace that line with the data in runtime.out.... and so on till it comes to current maintaince date 27 as no data is there for 27 in time.out the script have put it in the time.out

can anyone say me how i can replace the data in output file with data form input

Last edited by bhagya2340; 01-28-2009 at 05:00 PM..
# 2  
Old 01-28-2009
I've taken your input:

Quote:
Originally Posted by bhagya2340
20090124,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938,
20090125,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938,
20090126,00:02:33,00:00:35,8187,00:01:42,00:01:32,02:02:08,321055,
and created two files. The first test1.dat is as above. The second, test2.dat I took the first line above starting with "20090124" and changed that to "20090127" to spoof a date on the 27th, and moved it to the end so it looks like this:

Code:
20090125,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938, 
20090126,00:02:33,00:00:35,8187,00:01:42,00:01:32,02:02:08,321055, 
20090127,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938,

Then I ran the following 'comm' command:

Code:
comm test1.dat test2.dat | tr -d '\t'

and got the following output:

Code:
20090124,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938, 
20090125,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938, 
20090126,00:02:33,00:00:35,8187,00:01:42,00:01:32,02:02:08,321055,
20090127,00:02:31,00:00:11,2776,00:01:38,00:01:31,00:56:36,108938,

Which could be put into it's own file.

comm prints output in three columns. Col 1 is data only in the first file,
col 2 is data only in the second file and Col 3 is data in both files. The "columns" are created using tabs, and there is not more than one column's worth of data on a line. So by removing the tabs, I have output that lines up neatly, and gives me all the unique and non duplicated data from the previous two files.
# 3  
Old 01-28-2009
I am sorry to say but i dint understand anything....

I dont want to compare file and remove duplicates....i want to cheack if that pattern is already present in the file then remove it and replace it with the input....if not just place the input...
The partten here CUR_MAINT_DATE... i want something with grep, sid and awk...

Thanks for the help
# 4  
Old 01-28-2009
Quote:
Originally Posted by bhagya2340
I dont want to compare file and remove duplicates....i want to cheack if that pattern is already present in the file then remove it and replace it with the input....if not just place the input...
The partten here CUR_MAINT_DATE...
Seemed overkill to me to replace a line of data with an exact duplicate, when you can use a command that will get you only the newest data. 'comm' has options on the output.

Quote:
i want something with grep, sid and awk...
What if that made things more complicated than they need to be?

Granted I could be missing something that you either haven't mentioned, or I just don't undestand your explanation very well. But if I understand you correctly, the file that should have all the data added to it is 'time.out' and that data is coming from 'runtime.out'

If the structure of both files are the same as you posted in your first post, then the comm command will work for you.

Code:
comm -23 runtime.out time.out >> time.out

You don't even need to run it through tr to get rid of tabs, since only the data in runtime.out not appearing in time.out will be printed in the first column w/o any added tabs.
# 5  
Old 01-28-2009
Yes you are missing something that... time.out file is very old file and it have data form past 5 years... so it have to be the final file and the reason i have used this code is
echo >> time.out; tr -d '\n' < runtime.out >> time.out
because the data in runtime.out is like this
Code:
20090127,
00:02:33,
00:03:04,
62153,
00:01:39,
00:02:25,
01:39:55,
266355,
 ,
00:09:33,
00:00:22,
00:03:17,
00:00:02,
00:05:01,
 ,
 ,
 ,
00:03:21,
00:07:40,
 ,
00:03:38,
00:36:42,
 ,
 ,

# 6  
Old 01-28-2009
Quote:
Originally Posted by bhagya2340
Yes you are missing something that... time.out file is very old file and it have data form past 5 years... so it have to be the final file and the reason i have used this code is
echo >> time.out; tr -d '\n' < runtime.out >> time.out
because the data in runtime.out is like this
Okay, suspected the second part, but you hadn't mentioned the format of runtime.out before so there was no way to know.

As I used comm with the -23 option, it wouldn't care about the old data of time.out at all, so that data would be safe, it would simply get the newest data from runtime.out, but the different file structure is a deal breaker unless you want to create a temporary file to hold data that can be comm'd to time.out.

I realize now that the reason you append a new line before inserting the data from runtime.out piped through tr to time.out is because after the last insertion, there is no newline since tr removes all newlines. I assume this is done on each and every record, so that you do wind up with newlines after each record, except for the last record.

I'd advisde changing this order if possible so the last record has a newline appended to it also, or that would be a deal breaker if you try to find a solution involving 'sed', because sed ignores lines w/o a newline at the end.

So if you make the line look as follows:
Code:
tr -d '\n' < runtime.out >> time.out;echo >> time.out

And then at the command prompt, one time only, before you run the collection script.

Code:
$> echo >> time.out

Then your good to go if you want to find a sed solution. That, however will take more time than I have today for me to look into it.
# 7  
Old 01-29-2009
That really nice of you... thanks a lot...
I dont mind adding another tem file in between runtime.out and time.out if that will slove the problem... I just want some solution this as i have to forward the script by tomm morning to someone else...if not i will be a dealbreaker... I would prefer a sed... but i dont mind any other solution... till it works... please help me...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Awk replacing file with user input

this section of the awk code i have here takes file to work with from the user. the user specifies the file name from the command line and the file name is assigned to the variable $FLIST awk 'BEGIN { while((getline < "'${FLIST}'")>0) S FS="\n"; RS="}\n" } now, i dont want... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Replacing data in one file with data in another

Hello, I have 2 files delimited by "|". File1: 1|New York 12| Buffalo 599| Syracuse File2: 56 Kennedy |1 9 Burridge Pl|15 98 BELL ROCK |599 My goal: Is to replace the numerical numbers in "File 2" (second field, not street address) with the corresponding city names from... (2 Replies)
Discussion started by: vestport
2 Replies

4. Shell Programming and Scripting

adding data in input file if 2nd script output SUCCESS

Hi All, how can i edit my original data and add more data if my 2nd script outputs SUCESS? ex. input file: 1.txt nik,is,the 1script.sh if 2ndscript.sh output SUCCESS then i'm going to edit my input file and add data best,pogi.. sample outputdata. nik,is,the,best,pogi 2ndscript.sh... (3 Replies)
Discussion started by: nikki1200
3 Replies

5. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

6. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

7. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

8. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

9. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

10. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies
Login or Register to Ask a Question