update a file by key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting update a file by key
# 8  
Old 03-27-2007
ynixon, here is one more solution:
cut -d";" -f1,2 new_file > temp_new_file
egrep -v -f temp_new_file master_file > temp_egrep_file
cat temp_egrep_file new_file | sort > final_file
# 9  
Old 03-27-2007
Code:
awk 'BEGIN{FS=OFS=";"} {arr[$1";"$] = $3 } END{ for ( i in arr ) {print i,arr[i]}}' master newfile

# 10  
Old 03-28-2007
Quote:
Originally Posted by ynixon
It is even working faster Smilie
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", "m") || die "Unable to open file <$!>\n";

while(<FILE>) {
  chomp;
  @arr = split(/;/);
  $j = "$arr[0];$arr[1]";
  $fileHash{$j} = $arr[2];
}

close(FILE);

open(FILE, "<", "n") || die "Unable to open file <$!>\n";

while(<FILE>) {
  chomp;
  @arr = split(/;/);
  $j = "$arr[0];$arr[1]";
  if( exists $fileHash{$j} ) {
    $fileHash{$j} = $arr[2];
  }
  else {
    $fileHash{$j} = $arr[2];
  }
}

close(FILE);

foreach my $key ( keys %fileHash ) {
  print "$key;$fileHash{$key}\n";
}

exit 0

this should be even more faster Smilie
# 11  
Old 03-28-2007
Question Extended challenge

thank for you all - you are great

I improved the scenario:
1. the MASTER table contain the last timestamp of the update (field 4)
2. the NEW table remains the same - without any timestamp
3. the output timestamp should be updated only if changed

MASTER:
a;b;0;20060328114630
a;c;0;20060328114630
a;d;0;20060328114630

NEW:
a;b;0
a;c;1
a;e;2

the result should be:
a;b;0;20060328114630
a;c;1;20070328103926
a;d;0;20060328114630
a;e;2;20070328103926
# 12  
Old 03-28-2007
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", "m") || die "Unable to open file <$!>\n";

while(<FILE>) {
  chomp;
  @arr = split(/;/);
  $j = "$arr[0];$arr[1]";
  $fileHash{$j} = "$arr[2];$arr[3]";
}

close(FILE);

open(FILE, "<", "n") || die "Unable to open file <$!>\n";

while(<FILE>) {
  chomp;
  @arr = split(/;/);
  $j = "$arr[0];$arr[1]";
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $var = $arr[2] . ";" . (1900 + $year) . $mon . $mday . $hour . $min . $sec;
  if( exists $fileHash{$j} ) {
    @val = split(/;/, $fileHash{$j});
    $fileHash{$j} = $var if( $val[0] != $arr[2] );
  }
  else {
    $fileHash{$j} = $var;
  }
}

close(FILE);

foreach my $key ( keys %fileHash ) {
  print "$key;$fileHash{$key}\n";
}

exit 0

# 13  
Old 03-28-2007
Code:
awk -v dt="20070328103926" ' BEGIN{FS=OFS=";"} 
{ if( arr[$1";"$2] !~ "^"$3";") arr[$1";"$2]=$3";"( $4 !~ /^ *$/ ? $4 : dt ) }
END{ for ( i in arr ) {print i,arr[i]}}' master new

# 14  
Old 03-28-2007
Quote:
Originally Posted by anbu23
Code:
awk -v dt="20070328103926" ' BEGIN{FS=OFS=";"} 
{ if( arr[$1";"$2] !~ "^"$3";") arr[$1";"$2]=$3";"( $4 !~ /^ *$/ ? $4 : dt ) }
END{ for ( i in arr ) {print i,arr[i]}}' master new

the timestamp is not as expected
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getting count of Key in another file

Hi All, I need to find the count of key occurrence from one file corresponding to another file. Any help please. I am planning to read the CUST_ID in a while loop and do a grep , is there a way to do easier CUST_ID 677582806078 687582821181 687582828910 687582834580 687582834849... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

3. Shell Programming and Scripting

How to extract information a file according key id in another file?

hi, i have a large file containing the detailed information of a bunch of keys like this: JAT_0001 contig102_342_3_n2 contig102_342 atgcacgacta 30 50 20... (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

5. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

6. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

7. UNIX for Dummies Questions & Answers

Remove VI encryption key from file

Hi There, I have set encryption key to my file using :X command. Now that I no more need encryption key to the file, I just want to delete/remove the encryption key. I have gone through many source but in vain. None of the source provided me with the solution that I am looking for. I... (2 Replies)
Discussion started by: grc
2 Replies

8. Shell Programming and Scripting

Using Key to get data from second file

I posted a problem last week that had essentially two steps. Someone was kind enough to help me with the first step, but beacuse I didn't explain things well, left out the second step. I'm required to work in C Shell. I deeply appreciate any help, since I've never worked in a shell language... (4 Replies)
Discussion started by: bassmaster
4 Replies

9. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies
Login or Register to Ask a Question