bash - search and replace multiple occurrences -
so have file containing millions of lines.
, within file have occurrences such
=continent =country =state =city =street now have excel file in have text should replace these occurrences - example :
=continent should replaced =asia
other text
now thinking of writing java program read input file , read mapping file , each occurrence search , replace.
being lazy here - wondering if same using editors vim ? possible ?
note - dont want single text replace - have multiple text need found , replaced , dont want search , replace manually each.
edit1:
contents of file want replace: "1.txt"
continent=cont_text country=country_text the file contains values want replace : "to_replace.txt"
=cont_text~asia =country_text~india and using 'sed' here .sh file - doing wrong - not replace contents of "1.txt"
while ifs="~" read foo bar; echo $foo echo $bar filename in 1.txt; sed -i.backup 's/$foo/$bar/g;' $filename done done < to_replace.txt
you can't put $foo , $bar in single quotes because shell won't expand them. don't need for $filename in 1.txt loop because sed loop through lines of 1.txt. , can't use -i.backup inside loop because change backup file each time , not preserve original. script should be:
#!/bin/bash cp 1.txt 1.txt.backup while ifs="~" read foo bar; echo $foo echo $bar sed -i "s/$foo/=$bar/g;" 1.txt done < to_replace.txt output:
$ cat 1.txt continent=asia country=india
Comments
Post a Comment