Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace the strings begin with alg::switch_key_ by alg::dms::switch_key_ via gawk which is seen on Ubuntu16.04?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading