I want to find the difference between two files, only for the header (column names)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I want to find the difference between two files, only for the header (column names)
# 1  
Old 09-10-2014
I want to find the difference between two files, only for the header (column names)

Hi All,

I want to find the difference between two files, by checking only the headers (column names) and report if any new column is added in the latest file.

For Ex: If the file "declartion.txt has these columns

Code:
url;image;id;showcase_id;showcase_name

and the actual file "feed.txt" has a newly added column either in the middle or in the last like

Code:
url;image;id;showcase_id;colour;showcase_name

The declaration file which will have only the header (column names), but the "feed.txt" will have column names and the values for those columns under each column. Comparison is required only for the column names (header) and not any values.

After the comparison the shell script should should report the newly added column For Ex: "colour" in a file called "report_column_mis-match.txt". Once the report file is created I the shell script should further remove the new column which is added by the data provider along with it's values.

The data provider is adding columns each now and then not informing us in advance which is creating the problem while loading the file in to mysql database. since the values get loaded haywire when the new column added in the data file but the same is not added in the mysql table.

I finding this bit complicated since I am not a shell script expert. Please help.

Thanks in advance.

Regards
Praveen

Moderator's Comments:
Mod Comment Please use CODE tags as in the Forum Rules for code, files, input & output/errors
They make it clearer to read and preserves multiple spaces and other critical information about the content

Last edited by rbatte1; 09-10-2014 at 08:42 AM.. Reason: Added CODE tags. Infraction already issued.
# 2  
Old 09-10-2014
What have you tried so far?
# 3  
Old 09-10-2014
Hi Don Cragun,

I am still doing some research on this to get the clue.

Regards
Praveen

---------- Post updated at 04:46 AM ---------- Previous update was at 03:33 AM ----------

I am able to read the first line from feed.txt and compare it with declaration.txt and print the difference.

But I am still not able to find the position of the column and its values and remove it. Not sure how to handle multiple new columns and remove it, if found.

Please help.

Thanks & regards
Praveen
# 4  
Old 09-10-2014
Hi Praveen,

Just a couple of quick questions, does the column always appear in the same place along with it's data.

Can you show us an example of the data?

Regards

Dave

Last edited by rbatte1; 09-10-2014 at 08:43 AM..
# 5  
Old 09-10-2014
Take this as a starting point:
Code:
awk -F\; 'NR==FNR               {split ($0, HD)
                                 DEC=$0
                                 next
                                }
          FNR==1 && $0==DEC     {exit}
          FNR==1                {for (i=1; i<=NF && $i==HD[i]; i++);
                                 print "Column " i ": " $i > "mismatch.txt"}
                                {$i=""; sub (/;;/,";"); sub (/^;|;$/, "")
                                }
          1
        ' OFS=";" declartion.txt feed.txt

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-10-2014
Hi Rudic,

But it is not listing the newly added column. If the above script is run, it is giving the output as

Column 1: url,dealerid,dealer_name,location,zip,make

in mismatch.txt

As explained above, my need is to find the new columns (in this case two new columns url and make) and remove them from the feed.txt along with it's respective values.

Thanks & Regards
Praveen
# 7  
Old 09-10-2014
Quote:
Originally Posted by Praveen Pandit
Hi Rudic,

But it is not listing the newly added column.
Well, it does for the files you gave in your spec in post #1. With declartion.txt
Code:
url;image;id;showcase_id;showcase_name

and feed.txt
Code:
url;image;id;showcase_id;colour;showcase_name
url1;image1;id1;showcase_id1;colour1;showcase_name1
url2;image2;id2;showcase_id2;colour2;showcase_name2
url3;image3;id3;showcase_id3;colour3;showcase_name3
url4;image4;id4;showcase_id4;colour4;showcase_name4

it has stdout as
Code:
url;image;id;showcase_id;showcase_name
url1;image1;id1;showcase_id1;showcase_name1
url2;image2;id2;showcase_id2;showcase_name2
url3;image3;id3;showcase_id3;showcase_name3
url4;image4;id4;showcase_id4;showcase_name4

and mismatch.txt as
Code:
Column 5: colour

Quote:
If the above script is run, it is giving the output as

Column 1: url,dealerid,dealer_name,location,zip,make

in mismatch.txt

As explained above, my need is to find the new columns (in this case two new columns url and make) and remove them from the feed.txt along with it's respective values.

Thanks & Regards
Praveen
As explained above, it does what was specified, and you obviously changed the input file structure.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Prefix a variable in the first column of all the records of the files with and without header

In a bash shell, I have to prefix a variable to two .CSV files File1.CSV and File2.CSV. One of the files has a header and the other one is with no header in the below format: "value11","value12","value13","value14","value15","value16" "value21","value22","value23","value24","value25","value26"... (7 Replies)
Discussion started by: dhruuv369
7 Replies

2. Shell Programming and Scripting

How to get difference of the same column between two files when other column matches?

File 1: 20130416,235800,10.78.25.104,BR2-loc,60.0,1624,50.0,0,50.0,0 20130416,235800,10.78.25.104,BR1-LOC,70.0,10,50.0,0,70.0,0 20130416,235800,10.78.25.104,Hub_None,60.0,15,60.0,0,50.0,0 File 2: 20130417,000200,10.78.25.104,BR2-loc,60.0,1626,50.0,0,50.0,0... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

3. Homework & Coursework Questions

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1 AAA... (1 Reply)
Discussion started by: shakthi666
1 Replies

4. Shell Programming and Scripting

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1... (1 Reply)
Discussion started by: shakthi666
1 Replies

5. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

find difference in file column...

Hi All, i have a file that is tab delimited. i need help to find the rows which are having same price based on the site code but some times, there are difference so i need to find only the records which are different in all site code. Dept Sec Barcode 10001 10002 10003 10004... (1 Reply)
Discussion started by: malcomex999
1 Replies

9. Shell Programming and Scripting

script to compare first column of two files and find difference

Hi, I want to write a script which will compare the 1st column of both the files and will give the difference. e.g:- my 1st file contains: 89 /usr 52 /usr/local 36 /tmp 92 /opt 96 /home 27 /etc/opt/EMCom 1 ... (3 Replies)
Discussion started by: adityam
3 Replies

10. Shell Programming and Scripting

Column names in flat files

Hi all, I want to create column names in a flat file and then load the data through some other application. For example, I have a file with emp.txt and I need column names as eno,ename,sal in the first line. The delimiter here is comma and record delimiter is end of line or unix new line. Could... (1 Reply)
Discussion started by: srivsn
1 Replies
Login or Register to Ask a Question