2

Is there a command line way to change text in the file? Like say I would like to change all strings "lukuunottamatta" to the form "lukuun ottamatta" and "Lukuunottamatta" to the form "Lukuun ottamatta".

1 Answers1

2

sed will do what you're asking: http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html

e.g. sed -i 's/lukuunottamatta/lukuun ottamatta/g' /home/user/myfile.txt

It's important to note that the -i option (which allows you to make changes to a file in-place) doesn't exist for all versions of sed. If you're using an older version, you may need to write the changed version to a temporary file, then copy (or move) the temporary file contents into the actual file.

Here is an example from start to finish to do what you're asking:

echo "some stuff" > myfile.txt
echo "some more stuff" >> myfile.txt
echo "lukuunottamatta" >> myfile.txt
echo "and yet some more stuff" >> myfile.txt
sed -i 's/lukuunottamatta/lukuun ottamatta/g' myfile.txt
cat myfile.txt (and view that lukuunottamatta was indeed replaced with lukuun ottamatta)
CptSupermrkt
  • 1,492