linux - Finding the pattern and replacing the pattern inside the file using unix -
i need in unix.i have file have value declared , and have replace value when called. example have value &abc , &ccc. have substitute value of &abc , &ccc in place of them shown in output file.
input file
go &abc=ddd; if file found &ccc=10; no value name &abc; , age &ccc;
output:
go &abc=ddd; if file found &ccc=10; value name ddd; , age 10;
try using sed.
#!/bin/bash # input file command line argument. input_file="${1}" # map of variables values declare -a value_map=( [abc]=ddd [ccc]=10 ) # loop on keys in our map. variable in "${!value_map[@]}" ; echo "replacing ${variable} ${value_map[${variable}]} in ${input_file}..." sed -i "s|${variable}|${value_map[${variable}]}|g" "${input_file}" done
this simple bash script replace abc ddd , ccc 10 in given file. here example of working on simple file:
$ cat file.txt boo aaa abc duh abc ccc abcccc hmm $ ./replace.sh file.txt replacing abc ddd in file.txt... replacing ccc 10 in file.txt... $ cat file.txt boo aaa ddd duh ddd 10 ddd10 hmm
Comments
Post a Comment