How to compare 2 files and update one?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare 2 files and update one?
# 1  
Old 08-15-2013
Linux How to compare 2 files and update one?

Hi,

I have got 2 files which i need to compare and append based on the below conditions.

file 1:

File 1 has data in the following format

Code:
4300 2356
C234 5689
5500 2345

File 2 has data in the same fomat

Code:
4300 49
5555 12345

Now i need to compare the first 4 bytes in each line from file2 and see if it exists on file1, and if they match ( they may be present in any position), i need to append the values present in pos6 onwards in sorted order

For example, for the number 4300 the value should come as

4300 234569

and if the number in the 1st 4 bytes of file 2 is not found in file 1 , i need to append the whole line to file 1.

How can this be done......need help....anyone plzzz SmilieSmilieSmilieSmilie

Last edited by jim mcnamara; 08-15-2013 at 05:34 PM..
# 2  
Old 08-15-2013
Try this as a starting point; the sort of $2 you need to compose yourself:
Code:
sort file1 file2 |
awk     '$1 == SV1              {SV2=SV2 $2;next}
         NR>1 && $1 != SV1      {print SV1, SV2}
                                {SV1=$1;SV2=$2}
         END                    {print SV1,SV2} 
        '

# 3  
Old 08-15-2013
try:
Code:
awk '
function srt(ir, ln) {
   do {
      upd = 0;
      for(i=1; i < ln; i++) {
         if ( ir[i] > ir[i+1] ) {
            t = ir[i];
            ir[i] = ir[i+1];
            ir[i+1] = t;
            upd = 1;
         }
      }
   } while ( upd == 1 )
}
FNR==NR {a[$1]=$2; next}
a[$1] {
   $2=a[$1] $2;
   for (i in ar) delete ar[i];
   al=length($2);
   for (i=1; i<=al; i++) ar[i]=substr($2,i,1);
   srt(ar, al); s="";
   for (i=1; i<=length($2); i++) s=s ar[i];
   $2=s;
}
1
' file2 file1

for GNU style awks try:
Code:
awk '
FNR==NR {a[$1]=$2; next}
a[$1] {
   $2=a[$1] $2;
   split($2,ar, "");
   asort(ar); s="";
   for (i=1; i<=length($2); i++) s=s ar[i];
   $2=s;
}
1
' file2 file1


Last edited by rdrtx1; 08-15-2013 at 07:07 PM..
# 4  
Old 08-21-2013
can you please explain the above code???
The code given by Rudy is good....but i need a single code which will do the sorting of data col 6 onwards too......SmilieSmilie
# 5  
Old 08-21-2013
Before the print statements, you can n=split (SV2, TMP, "");, call a sort function (like the one by rdrtx1) for TMP, and then, instead SV2, print TMP[i] in a loop 1..n.

Last edited by RudiC; 08-21-2013 at 04:10 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need solution to compare two file and update and append the output

Hi All, I have two files File1 frame,007C1 server1_Parent frame,007C3 server2_Silver frame,007EE server3_Bronze frame,00855 server4_Parent frame,00856 server4_Parent frame,00858 server5_Parent frame,008FA server6_Silver frame,008FB server6_Silver frame,008FC server6_Silver... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

3. UNIX for Dummies Questions & Answers

Compare two flat files and update one based on the values in the other

Hi, I'm a newbie to scripting and am trying to compare two files using awk. The files are exactly the same dimensions. Where the first file has 0's I would like to create an updated version of the second file which has the corresponding elements set to zero also. eg: file1: 12345 1 2 0... (3 Replies)
Discussion started by: kasan0
3 Replies

4. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

5. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

6. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

7. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

8. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

9. Shell Programming and Scripting

How to compare data in two flat files and update them?

Hi All, I am giving an example similar to the problem I have. I have two data files of 10 columns each in which fields are delimited by comma(,). I need to compare compare the two files using the uniq col(col3). If there are any records in file1 and are not in file2 then I have check the value... (3 Replies)
Discussion started by: rajus19
3 Replies

10. UNIX for Dummies Questions & Answers

compare update time of files

Hi, does anyone know of a way to compare files update time (not only days - also hours and minutes) (command? scripts? perl scripts?) Dori (8 Replies)
Discussion started by: dorilevy
8 Replies
Login or Register to Ask a Question