Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Posts
    21

    Unanswered: search and insert

    I have file contain (FILE.txt)
    1:Bob Yarncbiy:sun201:bob Jon
    1:Bill Yoncbiy:ux10:Joe De
    1teve Zolo:secstc:sun221:Peter Green
    2:xxxxx:xxxxx:xxxx:xxx
    5:yyyy:yyy:yyy

    I have also another file contain system name Operating system name and the version of the operating system(DATA.txt)
    SUN201:MNK:zeep.com:8876geunOS:5.8tsall
    SUN221:MNK:zeep.com:8jjgeunOS:5.9ttk
    UX101:MNK:zeep.com:8876geunOS:6.1toodk

    I want to for every line in FILE.txt start with (1 get the operating system and version of the operating from file (DATA.txt) and insert them at the end of the line start with (1 on the same FILE.txt
    So FILE.txt will be
    1:Bob Yarncbiy:sun201:bob Jon:: SunOS::5.8
    1:Bill Yoncbiy:ux10:Joe De:: SunOS:6.1
    1teve Zolo:secstc:sun221:Peter Green:: SunOS:5.9
    Last edited by melindaj; 06-01-04 at 22:39.

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    #! /usr/bin/perl
    
    use strict;
    
    my $in_file = 'file.txt';
    my $data_file = 'data.txt';
    my @lines = ();
    my %os_hash = ();
    
    open(R_DATA,"<$data_file") or die $!;
    while(my $line = <R_DATA>){
      my @infos = split(/:/,$line);
      my $os = lc $infos[0];
      $os_hash{$os} = $infos[4].':'.$infos[5];
    }
    close R_DATA;
    
    open(R_FILE,"<$in_file") or die $!;
    while(my $line = <R_FILE>){
      if($line =~ /^1:/){
        chomp $line;
        my $os = (split(/:/,$line))[3];
        $os = lc $os;
        $line .= ':'.$os_hash{$os}."\n";
      }
      push(@lines,$line);
    }
    close R_FILE;
    
    open(W_FILE,">$in_file") or die $!;
    print W_FILE $_ for(@lines);
    close W_FILE;
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Jun 2004
    Posts
    21
    Yes Thank You.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •