I have a file that contains the map between the words. I have to refer to that file and replace those words with the mapped ones in some files. For example, below file has the table of words that are mapped like
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
I will have many files that has those key words (1.12.2.*). I want to search for these key words and replace those words with the corresponding mapping taken from this file. How to do this in shell. Suppose a file contains the following lines say
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.
After executing the script, the Numbers "1.12.2.12" and "1.12.2.4" should be replaced by 5 and 4 (referred from the master file). Can anyone help me out?
One way using GNU awk
:
awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt
Results:
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.
To save output to a file:
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
Explanation:
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'
Adding word boundaries makes the matching more strict:
awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub("\\<"i"\\>", array[i]) }1' master.txt file.txt
You could have sed
write a sed
script for you:
The mappings:
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
Input file:
cat << EOF > infile
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.
EOF
Generate a script based on the mappings (GNU sed):
sed -r -e 's:([^ ]*) +(.*):s/\\b\1\\b/\2/g:' mappings
Output:
s/\b1.12.2.4\b/1/g
s/\b1.12.2.7\b/12/g
s/\b1.12.2.2\b/5/g
s/\b1.12.2.4\b/4/g
s/\b1.12.2.6\b/67/g
s/\b1.12.2.12\b/5/g
Evaluate with another sed
(GNU sed):
sed -r -e 's:([^ ]*) +(.*):s/\\b\1\\b/\2/g:' mappings | sed -f - infile
Output:
The Id of the customer is 5. He is from Grg.
The Name of the machine is ASB
The id is 1. He is from Psg.
Note that the mappings are treated as regular expressions, e.g. a dot (.
) can mean any character, and may need escaping either in the mappings file or when generating the sed
script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With