update a file by key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting update a file by key
# 22  
Old 03-28-2007
Quote:
Originally Posted by ynixon
sorry you are right Smilie , It is OK .
how can I sort the output within the awk ?
Smilie what is faster " awk '{.....}' | sort " or in the awk itself ?
working fine, BUT when the files has many rows I get error:
>wc -l master
119 master
>wc -l new
1057 new
>awk -v dt="20070328103926" ' BEGIN{FS=OFS=";"} { if( arr[$1";"$2] >
awk: There is a regular expression error.
Invalid pattern.
The input line number is 10. The file is master.
The source line number is 1.
Smilie
# 23  
Old 03-28-2007
Quote:
Originally Posted by matrixmadhan
what I had posted ( in perl ) gives the output expected, what is the need to sort, you havent mentioned that ?
the whole output is needed to be sorted, I mannaged to sort.

My code is in ksh, how can I embed this perl and pass MASTER and NEW variables from the ksh to the perl ?
# 24  
Old 03-29-2007
Quote:
Originally Posted by ynixon
the whole output is needed to be sorted, I mannaged to sort.

My code is in ksh, how can I embed this perl and pass MASTER and NEW variables from the ksh to the perl ?
Just change it as,

Code:
#! /opt/third-party/bin/perl

open(FILE, "<", $ARGV[0]) || 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, "<", $ARGV[1]) || 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

and run it as,

Code:
perl script.pl MASTER NEW

# 25  
Old 03-29-2007
Quote:
Originally Posted by ynixon
working fine, BUT when the files has many rows I get error:
>wc -l master
119 master
>wc -l new
1057 new
>awk -v dt="20070328103926" ' BEGIN{FS=OFS=";"} { if( arr[$1";"$2] >
awk: There is a regular expression error.
Invalid pattern.
The input line number is 10. The file is master.
The source line number is 1.
Smilie
Can you show the tenth line in the master file?
# 26  
Old 03-29-2007
Quote:
Originally Posted by anbu23
Can you show the tenth line in the master file?
>head -10 master
b2b;Listener;0;20070329145000
b2b;Database;0;20070329145000
b2b;misplaced_synonyms;0;20070329145000
b2b;monitor_bb_query;0;20070329145000
b2b;system_def_tbs;0;20070329145000
b2b;wrong_tmp_tbs;0;20070329145000
b2b;partition_reach_maxvalue;0;20070329145000
b2b;unauth_dba_role;0;20070329145000
b2b;chk_offline_rbs;0;20070329145000
b2b;failed_broken_jobs;0;20070329145000

>head -10 new
b2b;Listener;0
b2b;Database;0
b2b;misplaced_synonyms;0
b2b;monitor_bb_query;0
b2b;system_def_tbs;0
b2b;wrong_tmp_tbs;0
b2b;partition_reach_maxvalue;0
b2b;unauth_dba_role;0
b2b;chk_offline_rbs;0
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