- From the desktop, press Alt+F2 to launch the Run Application window.
- Type: gnome-terminal then press Enter. This will launch a user terminal (similar to Windows command prompt).
- In the terminal window, type:cd ~then press Enter to change directory into your home directory.
- Next, we will create a test directory where you will be able to test the commands without impacting your other files. In the terminal, type:mkdir testthen press Enter.
- Now
cdinto your new directory.cd test - Download the test file from the Attachments section (located at the bottom of this article) and save it to your test directory.
- View the contents of this file by typing:cat replace.txt
- Before running the script, be certain that you are in your test directory. At the terminal, type:pwdthen press Enter. This prints the working directory which should be
test. Also, type: ls to verify that replace.txt is in the test directory. - Type the following into the terminal:perl -pi -e 's/red/truck/g' *.txt then press Enter.
- Now type:cat replace.txtAll occurences of the string: "red" have been replaced with the string: "truck".
- The replacement also works for more than one word. For example:perl -pi -e 's/green truck/boat/g' *.txtwill search for the string "green truck" and replace each instance with "boat".