i have 2 files first 1 has 2 lines: first line integer , second string. second file string corresponding number, want replace string in file 1 corresponding number in file 2 (like output file after file 2):
file 1:
51.6659 28.4185 chahp sanap
i want replace digit instead of string:
file 2:
kard 28.5581 51.6588 chah 28.4683 51.6566 sana 28.4513 51.6041
what want have:
output:
51.6659 28.4185 28.4683 51.6566 --- 51.6659 28.4185 28.4513 51.6041
try program, maybe give idea on how started.
more perl basics: https://www.perl.org/books/beginning-perl/
#!/usr/bin/perl # test.pl use strict; use warnings; # 1. assign files $file1 = './file1.txt'; $file2 = './file2.txt'; # 2. read files @file1_arr; open(my $fh, "<", $file1) or die "can't open $file1: $!"; while (my $row = <$fh>) { chomp $row; push(@file1_arr, $row); } close($fh); @file2_arr; open(my $fh2, "<", $file2) or die "can't open $file2: $!"; while (my $row = <$fh2>) { chomp $row; push(@file2_arr, $row); } close($fh2); # 3. replace string in file1 corresponding number in file 2 # create table @file2_arr %name_ha; foreach $i (@file2_arr) { @arr = split(/\s+/,$i); $key = $arr[0] . 'p'; # modify chah chahp... $value = "$arr[1] $arr[2]"; $name_ha{$key} = $value; } # replace... @str_arr = split(/\s+/, $file1_arr[1]); # chahp sanap @str_res; foreach $i (@str_arr) { push(@str_res, $name_ha{$i}) if (defined $name_ha{$i}); } # print result $res; foreach $i (@str_res) { $res .= "$file1_arr[0]\n"; $res .= "$i\n---\n"; } print $res; # 4. write result new file open(my $fh3, ">", "./output.txt") or die "can't open > ./output.txt: $!"; print $fh3 $res; close($fh3); print "result saved in ./output.txt\n"; # output: # [~]$ ./test.pl # 51.6659 28.4185 # 28.4683 51.6566 # --- # 51.6659 28.4185 # 28.4513 51.6041 # --- # result saved in ./output.txt # [~]$ cat ./output.txt # 51.6659 28.4185 # 28.4683 51.6566 # --- # 51.6659 28.4185 # 28.4513 51.6041 # --- # [~]$