Complicated(?) text file comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complicated(?) text file comparison
# 1  
Old 02-14-2009
Complicated(?) text file comparison

I've got two files, both plain text. Each file is a datafeed of products, pipe delimited. The current file is in directory 1 and yesterday's file is in directory 2 (literally, those are the directory names). What I'm trying to do is compare the files and pull out products whose price has changed since yesterday and products that are new since yesterday. What I've come up with so far is a basic comparison to see if *anything* is different:

comm -23 1/datafeed.txt 2/datafeed.txt > 1/changes.txt

and that's a good start. It tells me, for example, when there's new products since yesterday, but it also tells me if they fixed a spelling error in one of the fields, which I don't really care about. Here's an abbreviated version of what these files look like:

Today's datafeed file:

ProductID|Name|Price|Color
84837423|Woot|2.50|Red
37238528|Blah|3.50|Blue
23452323|Rush|4.99|Black

Yesterday's datafeed file:

ProductID|Name|Price|Color
84837423|Woot|1.50|Red
37238528|Blah|3.50|Bluu

So, ideally, I'd like to have a newItems.txt file with the "Rush" line from today's datafeed and a changedPrices.txt file with the "Woot" line from today's datafeed. The "Blah" line, where they corrected a spelling error, doesn't interest me.

Thanks for any pointers you can give!

~Daniel
# 2  
Old 02-14-2009
Code:
awk -F \| '
NR == FNR  { product[$1] = $0; next }
! product[$1] { print > "newitems.txt" }
product[$1] {
  split(product[$1],f,"|")
  if ( f[3] != $3 ) print > "changedPrices.txt" }
' yesterday.txt today.txt

# 3  
Old 02-14-2009
Absolutely perfect, thank you! I changed the file names, plugged it into my script and got exactly what I was looking for - and even better, I think I understand how it works. I love learning new things.

Thanks again!
# 4  
Old 02-16-2009
Code:
#!/usr/bin/perl
open FH,"<old.txt";
while(<FH>){
	my @tmp=split("[|]",$_);
	$hash{$tmp[1]}->{PRC}=$tmp[2];
	$hash{$tmp[1]}->{DATA}=$_;
}
close FH;
open N_FH,">newitem.txt";
open C_FH,">changed.txt";
open FH,"<new.txt";
while(<FH>){
	my @tmp=split("[|]",$_);
	if (not exists ($hash{$tmp[1]})){
		print N_FH $_ ;
		next;
	}
	print C_FH $_ if $hash{$tmp[1]}->{PRC} ne $tmp[2];
}
close FH;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Text file parsing and comparison

I have two files (first.txt and second.txt): more first.txt cat mammal lizard reptile Elephant mammal ant Insecta more second.txt ant termite ant army_ant (9 Replies)
Discussion started by: cs_novice
9 Replies

2. Shell Programming and Scripting

text file comparison

Hi All, I would like to write script which will compare two text files, however the order of the content in the files might change, for eg, File 1 File 2 ------- -------- ABC ABC DEF GHI GHI ... (3 Replies)
Discussion started by: Sub.kalps
3 Replies

3. Shell Programming and Scripting

Please help. Complicated text file manipulation problem

Let me try my best to give you a picture of what I'm trying to do. Once again I'm sorry for the essay thats coming up. I programmed a rather large library of script functions to deal with input, displaying ANSI block graphics, playing sounds, and refining the terminal and so on. I also designed... (8 Replies)
Discussion started by: tinman47
8 Replies

4. Shell Programming and Scripting

Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum :b::D: I recently tried something like this, I want to replace all those numberings/letters that are located between <string>file://localhost/var/mobile/Applications/ and /Documents/</string> numberings =---- replace with: first... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

Parsing complicated CSV file with sed

Yes, there is a great doc out there that discusses parsing csv files with sed, and this topic has been covered before but not enough to answer my question (unix.com forums). I'm trying to parse a CSV file that has optional quotes like the following: "Apple","Apples, are fun",3.60,4.4,"I... (3 Replies)
Discussion started by: analog999
3 Replies

6. Shell Programming and Scripting

Search complicated strings on file

Can someone help me? I been figuring out how I can search and extract a complicated search string from a file. The whole string is delimited by a period. And the file where I'm searching is composed of differnt string such as that. For example, I have this search string: and I have a file... (3 Replies)
Discussion started by: Orbix
3 Replies

7. Shell Programming and Scripting

Parsing a Complicated properties file

Hi All, I have a requirement to parse a file. Let me clear you all on the req. I have a job which contains multiple tasks and each task will have multiple attributes that will be in the below format. Each task will have some sequence number according to that sequence number tasks shld... (1 Reply)
Discussion started by: rajeshorpu
1 Replies

8. Shell Programming and Scripting

need help with script - output to file, but complicated

ok, so what i want to do is make a script that will do the following: take out from a command in the terminal put that output into a text file already on my computer. the only thing is that i need to put the output in the file kinda weird: i need to take it and put each character of output... (13 Replies)
Discussion started by: twoodcc
13 Replies

9. Shell Programming and Scripting

complicated search within file

Hi, I have following problem. I have a file with time stamps and some data describing what happened between time stamps. Something like this: 10:00 meeting with K meeting with L 11:00 lunch 12:00 work with K 13:00 From this file I have to get a file with... (7 Replies)
Discussion started by: mmike
7 Replies

10. Shell Programming and Scripting

Complicated string searching in a file

Hi folks, Following a part of opmn.xml file: <process-type id="OC4J_RiGHTv_PLATOR81" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (5 Replies)
Discussion started by: nir_s
5 Replies
Login or Register to Ask a Question