As what said in the title, I want to replace alg::switch_key_hash to alg::dms::switch_key_hash, alg::switch_key_sort to alg::dms::switch_key_sort and etc via awk which actually uses gawk in Ubuntu16.04.
>Solution :
To replace strings that begin with alg::switch_key_ with alg::dms::switch_key_ using gawk (GNU awk), you can use the gsub function, which performs global substitution on each input record. Here’s how you can do it:
gawk '{gsub(/alg::switch_key_/, "alg::dms::switch_key_"); print}' inputfile
This command will read from inputfile, replace all occurrences of alg::switch_key_ with alg::dms::switch_key_, and then print the modified lines.
If you want to perform this replacement on multiple files and overwrite them, you can use a loop in the shell. For example:
for file in *.txt; do
gawk -i inplace '{gsub(/alg::switch_key_/, "alg::dms::switch_key_"); print}' "$file"
done
This will process each .txt file in the current directory, replacing the specified string as needed. The -i inplace option tells gawk to edit the files in place.
If you do not want to overwrite the original files, you can redirect the output to new files:
for file in *.txt; do
gawk '{gsub(/alg::switch_key_/, "alg::dms::switch_key_"); print}' "$file" > "${file}.new"
done
This will create new files with the same name as the original files but with .new appended to their names.
I hope this helps!