Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. 1.12.2.4 1
  2. 1.12.2.7 12
  3. 1.12.2.2 5
  4. 1.12.2.4 4
  5. 1.12.2.6 67
  6. 1.12.2.12 5
  7.  
  8. awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt
  9.  
  10. The Id of the customer is 5. He is from Grg.
  11. The Name of the machine is ASB
  12. The id is 4. He is from Psg.
  13.  
  14. 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
  15.  
  16. FNR==NR { ... } # FNR is the current record number, NR is the record number
  17. # so FNR==NR simply means: "while we process the first file listed
  18. # in this case it's "master.txt"
  19. array[$1]=$2 # add column 1 to an array with a value of column 2
  20. next # go onto the next record
  21.  
  22. { # this could be written as: FNR!=NR
  23. # so this means "while we process the second file listed..."
  24. for (i in array) # means "for every element/key in the array..."
  25. gsub(i, array[i]) # perform a global substitution on each line replacing the key
  26. # with it's value if found
  27. }1 # this is shorthand for 'print'
  28.  
  29. awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub("\<"i"\>", array[i]) }1' master.txt file.txt
  30.  
  31. cat << EOF > mappings
  32. 1.12.2.4 1
  33. 1.12.2.7 12
  34. 1.12.2.2 5
  35. 1.12.2.4 4
  36. 1.12.2.6 67
  37. 1.12.2.12 5
  38. EOF
  39.  
  40. sed -r -e 's:([^ ]*) +(.*):s/1/2/g:' mappings
  41.  
  42. sed -r -e 's:([^ ]*) +(.*):s/\b1\b/2/g:' mappings | sed -f - infile
  43.  
  44. > cat temp
  45. 1.12.2.4 1
  46. 1.12.2.7 12
  47. 1.12.2.2 5
  48. 1.12.2.4 4
  49. 1.12.2.6 67
  50. 1.12.2.12 5
  51.  
  52. > cat temp2
  53. The Id of the customer is 1.12.2.12. He is from Grg.
  54. The Name of the machine is ASB
  55. The id is 1.12.2.4. He is from Psg.
  56.  
  57. > temp.pl
  58. The Id of the customer is 5. He is from Grg.
  59. The Name of the machine is ASB
  60. The id is 4. He is from Psg
  61.  
  62. >
  63.  
  64. #!/usr/bin/perl
  65.  
  66. use strict;
  67. use warnings;
  68.  
  69. my %hsh=();
  70.  
  71. open (MYFILE, 'temp');
  72. open (MYFILE2, 'temp2');
  73.  
  74. while (<MYFILE>) {
  75. my@arr = split/s+/;
  76. $hsh{$arr[0]} = $arr[1];
  77. }
  78. my $flag;
  79. while(<MYFILE2>)
  80. {
  81. $flag=0;
  82. my $line=$_;
  83. foreach my $key (keys %hsh)
  84. {
  85. if($line=~/$key/)
  86. {
  87. $flag=1;
  88. $line=~s/$key/$hsh{$key}/g;
  89. print $line;
  90. }
  91. }
  92. if($flag!=1)
  93. {
  94. print $line;
  95. $flag=0;
  96. }
  97. }
  98. close(MYFILE);
  99. close(MYFILE2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement