Skip to main content
Commonmark migration
Source Link

#Answer#

Answer

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=${line%% *};
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

#Explanation#

Explanation

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

#About parameter expansion#

About parameter expansion

In order to set the variables $oldw and $neww we have to get the first and last word of each line. For doing so, we are using parameter expansion (pure Bash implementation), although we could have used other ways to get the first and last word of the string (i.e. cut or awk).

  • ${line##* } : from variable line, remove largest prefix (double #) pattern, where pattern is any characters (*) followed by a space ( ). So we get the last word in line.
  • ${line%% *} : from variable line, remove largest suffix (double %) pattern, where pattern is a space ( ) followed by any characters (*). So we get the first word in line.

Words were separated by a space in this case, but we could have used any separator.

#Answer#

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=${line%% *};
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

#Explanation#

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

#About parameter expansion#

In order to set the variables $oldw and $neww we have to get the first and last word of each line. For doing so, we are using parameter expansion (pure Bash implementation), although we could have used other ways to get the first and last word of the string (i.e. cut or awk).

  • ${line##* } : from variable line, remove largest prefix (double #) pattern, where pattern is any characters (*) followed by a space ( ). So we get the last word in line.
  • ${line%% *} : from variable line, remove largest suffix (double %) pattern, where pattern is a space ( ) followed by any characters (*). So we get the first word in line.

Words were separated by a space in this case, but we could have used any separator.

Answer

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=${line%% *};
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

Explanation

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

About parameter expansion

In order to set the variables $oldw and $neww we have to get the first and last word of each line. For doing so, we are using parameter expansion (pure Bash implementation), although we could have used other ways to get the first and last word of the string (i.e. cut or awk).

  • ${line##* } : from variable line, remove largest prefix (double #) pattern, where pattern is any characters (*) followed by a space ( ). So we get the last word in line.
  • ${line%% *} : from variable line, remove largest suffix (double %) pattern, where pattern is a space ( ) followed by any characters (*). So we get the first word in line.

Words were separated by a space in this case, but we could have used any separator.

Better explanation
Source Link
Peque
  • 3.6k
  • 4
  • 31
  • 36

#Answer#

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=$(echo $line | cut -f1 -d '{line%% ')*};
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

#Explanation#

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

Not optimal, I know#About parameter expansion#

In order to set the variables $oldw and $neww we have to get the first and last word of each line. For doing so, we are using parameter expansion (pure Bash implementation), although we could have used other ways to get the first and last word of the string (i.e. :-Pcut or awk).

  • ${line##* } : from variable line, remove largest prefix (double #) pattern, where pattern is any characters (*) followed by a space ( ). So we get the last word in line.
  • ${line%% *} : from variable line, remove largest suffix (double %) pattern, where pattern is a space ( ) followed by any characters (*). So we get the first word in line.

PS: check inWords were separated by a test folder to see if it works!space in this case, but we could have used any separator.

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=$(echo $line | cut -f1 -d ' ');
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

#Answer#

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do
neww=${line##* };
oldw=${line%% *};
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;
done

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

#Explanation#

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

#About parameter expansion#

In order to set the variables $oldw and $neww we have to get the first and last word of each line. For doing so, we are using parameter expansion (pure Bash implementation), although we could have used other ways to get the first and last word of the string (i.e. cut or awk).

  • ${line##* } : from variable line, remove largest prefix (double #) pattern, where pattern is any characters (*) followed by a space ( ). So we get the last word in line.
  • ${line%% *} : from variable line, remove largest suffix (double %) pattern, where pattern is a space ( ) followed by any characters (*). So we get the first word in line.

Words were separated by a space in this case, but we could have used any separator.

Improved readability with code in multiple lines.
Source Link
Peque
  • 3.6k
  • 4
  • 31
  • 36

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do  
neww=${line##* };  
oldw=$(echo $line | cut -f1 -d ' ');  
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \;  
done

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do neww=${line##* }; oldw=$(echo $line | cut -f1 -d ' '); find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \; done

Basically:

  1. Cat file map.txt.
  2. Read each line and get word to be replaced $oldw and replacement $neww.
  3. For each pair, execute the find command you were already using.

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

If you are not concerned about speed (this is a one time task), then maybe you could try this:

cat map.txt | while read line; do 
neww=${line##* }; 
oldw=$(echo $line | cut -f1 -d ' '); 
find /some/folder -type f -exec sed -i "s/$oldw/$neww/g" {} \; 
done

Basically:

  1. Cat file map.txt.
  2. Read each line and get the word to be replaced $oldw and the replacement $neww.
  3. For each pair, execute the find command you were already using (notice the double quotes this time in order to allow variable substitution).

Not optimal, I know... :-P

PS: check in a test folder to see if it works!

Source Link
Peque
  • 3.6k
  • 4
  • 31
  • 36
Loading