Awk and sql field comparation help.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk and sql field comparation help.
# 1  
Old 03-30-2009
Awk and sql field comparation help.

Hi, before my question let me excuse myself for the bad grammar and possible confusion (i am used to write in spanish)*

I need some help, i'm making a query:

SELECT empnmbr as empl, origin as addr, old_fields_value as prev_sta, new_fields_value as new_sta \
FROM webapplog$bdate, datachangelog$bdate \
WHERE datachangelog$bdate.webapplog_oid = webapplog$bdate.oid \
AND datachangelog$bdate.table_name = 'address'
ORDER BY WEBAPPLOG_OID


which returns answers like:

empl | addr | prev_sta | new_sta
23424|3030303| 17,1,2,3,4,5,6,7,8|17,2,2,3,4,5,6,7,9
12345|home ad| 18,1,5,1,6,1,16,15|18,2,4,1,6,1,16,19


What i need is to read every line of the answer, compare the differences between the third and fourth fields for every line, and mark in which position of those fields are the differences.

so the answer will be more like:

empl | addr | prev_sta | new_sta | position
23424|3030303| 1 |2 | 2
23424|3030303| 8 |9 | 9
12345|home ad| 1 |2 | 2
12345|home ad| 5 |4 | 3
12345|home ad| 15 |19 | 8

but i have no idea what to do here. Maybe passing each field to an array and comparing arrays? the third and fourth fields of the original query have always the same number of elements (25) but sometimes only a single element is different.

Please any hint is helpful




*Si a alguno de los miembros del foro le parece mas simple puedo escribir en espanol y quiza sea mas claro el caso
# 2  
Old 03-30-2009
Code:
awk -F'|' 'NR==1{print $0, "position"; next}
           {n=split($3,a,","); split($4,b,",")
           while(++i<=n) if(a[i]!=b[i]) print $1,$2,a[i],b[i],i
           i=split("",a); split("",b)}' OFS='|' filename

# 3  
Old 03-30-2009
perl

Code:
sub comp{
	#print $_[0]," -- ",$_[1],"\n";
	my @a1=split(",",$_[0]);
	my @a2=split(",",$_[1]);
	my @arr;
	for(my $i=0;$i<=$#a1;$i++){
		push @arr, $a1[$i]."|".$a2[$i]."|".($i+1) if $a1[$i] != $a2[$i]
	}
	return \@arr;
}
open $fh,"<","a.spl" or die "Can not open file";
while(<$fh>){
	chomp;
	my @tmp=split("[|]",$_);
	if($tmp[2] ne $tmp[3]){
		my $ref=comp($tmp[2],$tmp[3]);
		map {print $tmp[0],"|",$tmp[1],"|",$_,"\n"} @$ref;
	}
}



awk
Code:
nawk -F"|" '{
	num=split($3,arr1,",")
	num=split($4,arr2,",")
	for(i=1;i<=num;i++){
		if(arr1[i]!=arr2[i])
			print $1"|"$2"|"arr1[i]"|"arr2[i]"|"i
	}
}' filename

# 4  
Old 03-31-2009
Thanks

thanks to all, you've helped me a lot, i was able to make it work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

5. Programming

SQL issues comparing Long field to sysdate

I am having hard time with this sql: select partition_name, high_value FROM user_tab_partitions WHERE table_name = 'WNP_TPRESPONSE_INSTANCE' and to_char(high_value) <= to_char(sysdate - 15, 'yyyymm') ; I get an error: ORA-00932: inconsistent datatypes: expected CHAR got LONG... (1 Reply)
Discussion started by: mrn6430
1 Replies

6. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

7. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

10. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies
Login or Register to Ask a Question