Skip to main content
added 15 characters in body
Source Link
Zombo
  • 1
  • 7
  • 47
  • 65

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x'g' -cx "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x'g' -cx {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x'g' -cx "$CHA"
done

replace only if the string is found in a certain context?

ex -sc ''g/DEL/s/ALF/BRA/g|x'g' -cx file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x'g' -cx file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|ECH/BRA/g|x'g' -cx file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/FOX/GOL/g|x'g' -cx file

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x' {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

replace only if the string is found in a certain context?

ex -sc '/DEL/s/ALF/BRA/g|x' file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x' file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|ECH/BRA/g|x' file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/FOX/GOL/g|x' file

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g' -cx "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g' -cx {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g' -cx "$CHA"
done

replace only if the string is found in a certain context?

ex -sc 'g/DEL/s/ALF/BRA/g' -cx file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g' -cx file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|ECH/BRA/g' -cx file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/FOX/GOL/g' -cx file
edited body
Source Link
Zombo
  • 1
  • 7
  • 47
  • 65

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x' {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

replace only if the string is found in a certain context?

ex -sc '/bazDEL/s/ALF/BRA/g|x' file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x' file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|BRA\vALF|ECH/DELBRA/g|x' file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/DELFOX/ECHGOL/g|x' file

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x' {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

replace only if the string is found in a certain context?

ex -sc '/baz/s/ALF/BRA/g|x' file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x' file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|BRA/DEL/g|x' file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/DEL/ECH/g|x' file

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x' {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

replace only if the string is found in a certain context?

ex -sc '/DEL/s/ALF/BRA/g|x' file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x' file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|ECH/BRA/g|x' file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/FOX/GOL/g|x' file
Source Link
Zombo
  • 1
  • 7
  • 47
  • 65

You can use Vim in Ex mode:

replace string ALF with BRA in all files in the current directory?

for CHA in *
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

do the same recursively for sub directories?

find -type f -exec ex -sc '%s/ALF/BRA/g|x' {} ';'

replace only if the file name matches another string?

for CHA in *.txt
do
  ex -sc '%s/ALF/BRA/g|x' "$CHA"
done

replace only if the string is found in a certain context?

ex -sc '/baz/s/ALF/BRA/g|x' file

replace if the string is on a certain line number?

ex -sc '2s/ALF/BRA/g|x' file

replace multiple strings with the same replacement

ex -sc '%s/\vALF|BRA/DEL/g|x' file

replace multiple strings with different replacements

ex -sc '%s/ALF/BRA/g|%s/DEL/ECH/g|x' file