Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.12.2.4 1
- 1.12.2.7 12
- 1.12.2.2 5
- 1.12.2.4 4
- 1.12.2.6 67
- 1.12.2.12 5
- awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt
- The Id of the customer is 5. He is from Grg.
- The Name of the machine is ASB
- The id is 4. He is from Psg.
- awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt > name_of_your_output_file.txt
- FNR==NR { ... } # FNR is the current record number, NR is the record number
- # so FNR==NR simply means: "while we process the first file listed
- # in this case it's "master.txt"
- array[$1]=$2 # add column 1 to an array with a value of column 2
- next # go onto the next record
- { # this could be written as: FNR!=NR
- # so this means "while we process the second file listed..."
- for (i in array) # means "for every element/key in the array..."
- gsub(i, array[i]) # perform a global substitution on each line replacing the key
- # with it's value if found
- }1 # this is shorthand for 'print'
- awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub("\<"i"\>", array[i]) }1' master.txt file.txt
- cat << EOF > mappings
- 1.12.2.4 1
- 1.12.2.7 12
- 1.12.2.2 5
- 1.12.2.4 4
- 1.12.2.6 67
- 1.12.2.12 5
- EOF
- sed -r -e 's:([^ ]*) +(.*):s/1/2/g:' mappings
- sed -r -e 's:([^ ]*) +(.*):s/\b1\b/2/g:' mappings | sed -f - infile
- > cat temp
- 1.12.2.4 1
- 1.12.2.7 12
- 1.12.2.2 5
- 1.12.2.4 4
- 1.12.2.6 67
- 1.12.2.12 5
- > cat temp2
- The Id of the customer is 1.12.2.12. He is from Grg.
- The Name of the machine is ASB
- The id is 1.12.2.4. He is from Psg.
- > temp.pl
- The Id of the customer is 5. He is from Grg.
- The Name of the machine is ASB
- The id is 4. He is from Psg
- >
- #!/usr/bin/perl
- use strict;
- use warnings;
- my %hsh=();
- open (MYFILE, 'temp');
- open (MYFILE2, 'temp2');
- while (<MYFILE>) {
- my@arr = split/s+/;
- $hsh{$arr[0]} = $arr[1];
- }
- my $flag;
- while(<MYFILE2>)
- {
- $flag=0;
- my $line=$_;
- foreach my $key (keys %hsh)
- {
- if($line=~/$key/)
- {
- $flag=1;
- $line=~s/$key/$hsh{$key}/g;
- print $line;
- }
- }
- if($flag!=1)
- {
- print $line;
- $flag=0;
- }
- }
- close(MYFILE);
- close(MYFILE2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement