Comparing two files field and position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files field and position
# 1  
Old 04-06-2016
Comparing two files field and position

I have two files : file1 and file2

file1 format:

Code:
7026-H70       7026-1017685
7026-H70       7026-1017687
7026-B80       7026-108D65A
7026-B80       7026-108D67A
7046-B50       7026-1034B4A

File2 format :

Code:
mt01cp01 7026-B80 01108D69A
mt01cp02 7026-B80 01108D68A
mt01sv01 7046-B50 011034B4A
mt02cp01 7026-B80 01108D67A


I would like to compare file1 column2 position 6 to 12 (eg. in first line 1017685) to file3 column3 position 3 to 9 (eg. first line 108D69A) to find the unmatch

The out put should come out like this:



Code:
1017685
1017687
108D65A
108D69A
108D68A

Thank you.


Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-06-2016 at 06:29 PM.. Reason: Added code tags
# 2  
Old 04-06-2016
Any attempts/ideas/thoughts from your side?
# 3  
Old 04-06-2016
Howsoever, try
Code:
awk 'FNR==NR {T[substr($2, 6)]++; next} {SRCH = substr ($3, 3)} SRCH in T {delete T[SRCH]; next} {T[SRCH]++} END {for (t in T) print t}' file1 file2
108D69A
1017685
1017687
108D65A
108D68A

# 4  
Old 04-06-2016
try also:
Code:
awk '
NR==FNR {s=substr($2,6,7); a[s]=s; c[s]=s; next }
{s=substr($3,3,7); b[s]=s; c[s]=s }
END {for (i in c) if (a[i]!=b[i]) print i}' file1 file2

# 5  
Old 04-12-2016
Thank you so much.
apparently
both code returning 3 digits


****returns three digits
xxx
xxx
xxx

---------- Post updated at 03:55 PM ---------- Previous update was at 09:27 AM ----------

I have to put the file in order; I got the correct out put .
Thanks.

Last edited by amir07; 04-12-2016 at 05:30 PM..
# 6  
Old 04-12-2016
to order output for gnu awk try:
Code:
awk '
NR==FNR {s=substr($2,6,7); a[s]=s; c[s]=s; next }
{s=substr($3,3,7); b[s]=s; c[s]=s }
END {n=asort(c); for (i=1; i<=n; i++) if (a[c[i]]!=b[c[i]]) print c[i]}' file1 file2

otherwise try:
Code:
awk '
NR==FNR {s=substr($2,6,7); a[s]=s; c[s]=s; next }
{s=substr($3,3,7); b[s]=s; c[s]=s }
END {for (i in c) if (a[i]!=b[i]) print i}' file1 file2 | sort

Make sure the input files order is not reversed. Otherwise 3 character output will occur.

Last edited by rdrtx1; 04-12-2016 at 07:13 PM..
# 7  
Old 04-13-2016
since the codes are long, I am saving it as a file (comp_host) and then trying to run it
I am running it on AIX

here I put it :

Code:
#! /bin/awk -f
awk '
NR==FNR {s=substr($2,6,7); a[s]=s; c[s]=s; next }
{s=substr($3,3,7); b[s]=s; c[s]=s }
END {for (i in c) if (a[i]!=b[i]) print i}'

I am running it as :

Code:
> comp_host file1 file

(nothing returns)

if I run
Code:
awk -f comp_host file1 file

I get an error

Code:
syntax error The source line is 3.
 The error context is
                 >>> ' <<< 
 awk: Quitting
 The source line is 3.


Thank you.

Last edited by Don Cragun; 04-13-2016 at 01:04 PM.. Reason: Add CODE and ICODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

2. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

3. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

4. Shell Programming and Scripting

Comparing two huge files on field basis.

Hi all, I have two large files and i want a field by field comparison for each record in it. All fields are tab seperated. file1: Email SELVAKUMAR RAMACHANDRAN Email SHILPA SAHU Web NIYATI SONI Web NIYATI SONI Email VIINII DOSHI Web RAJNISH KUMAR Web ... (4 Replies)
Discussion started by: Suman Singh
4 Replies

5. Shell Programming and Scripting

comparing files with field using awk

hi, i have 1 files a.csv temp.out a.cvs looks like add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-209 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-219 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-220 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-221 and temp.out looks... (1 Reply)
Discussion started by: raghavendra.cse
1 Replies

6. Programming

Zero Fill 10 position field

Hi everyone, I am using shell scripting on a Unix SCO box. It calls a C program named zerofill. I do not know anything about C. I am having an issue with a hash total that should only be zero filled 10 positions but the record has 11 positions. DEBUG RESULTS + nice -n -5 bc -l /tmp/fhash8395... (0 Replies)
Discussion started by: ski
0 Replies

7. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

8. Shell Programming and Scripting

filtering records based on numeric field value in 8th position

I have a ";" delimited file.Whcih conatins a number fileds of length 4 charcters in 8th position But there is a alphanumeric charcters like : space, ";" , "," , "/" , "23-1" , "23 1" , "aqjhdj" , "jun-23" , "APR-04" , "4:00AM" , "-234" , "56784 ", "." , "+" "_" , "&" , "*" , "^" , "%" , "!"... (2 Replies)
Discussion started by: indusri
2 Replies

9. Shell Programming and Scripting

Field comparing in files

Guys trying to compare field in two files. For Ex: demo.txt 23.33.4.2 hostname 3.2.4.2 hostname12 demo1.txt 3.3.3.3 hostname23 45.23.23.23 hostname 323 I would like to compare the ips b/w these two files.any... (2 Replies)
Discussion started by: coolkid
2 Replies

10. Shell Programming and Scripting

Last field problem while comparing two csv files

Hi All, I've two .csv files as below file1.csv abc, tdf, 223, tpx jgsd, tex, 342, rpy a, jdjdsd, 423, djfkld Where as file2.csv is the new version of file1.csv with some added fields in the end of each line and some additional lines. lfj, eru, 98, jkldj, 39, jdkj9 abc, tdf, 223, tpx,... (3 Replies)
Discussion started by: ganapati
3 Replies
Login or Register to Ask a Question