14

I wish to write a shell script whereby I could read the file from command and edit the file without manual intervention (based on searching some text and replacing it with another).

I need not use any text editor for this....simply text-searching (like using grep) and then replacing it with some other text and saving the changes....

muru
  • 207,228

5 Answers5

11

That is where sed comes in to play. A sed command has this format:

[pattern1][,pattern2][!] command [args]

It uses regexes so it can/will be a bit difficult. Some basic examples taken from the 2nd link below:

# substitute (find and replace) "foo" with "bar" on each line
sed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
sed 's/\(.*\)foo/\1bar/' # replace only the last case

# substitute "foo" with "bar" ONLY for lines which contain "baz"
sed '/baz/s/foo/bar/g'

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
sed '/baz/!s/foo/bar/g'

# change "scarlet" or "ruby" or "puce" to "red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
gsed 's/scarlet\|ruby\|puce/red/g' # GNU sed only

Some references

Rinzwind
  • 309,379
9

Very late answer. However, this might help others with a similar problem/question.

I would recommend creating and applying a patch. A nice example can be found here.

For example, assuming that a new.txt file contains changes that you want to apply to old.txt. You can execute the commands on a terminal or by creating and executing a patch_file.sh.

Command line: open a terminal and copy and execute the lines below (change the file names as necessary):

diff old.txt new.txt > patch.patch # to create the patch
patch old.txt -i patch.patch -o patched_old.text # to apply patch

Script: using an .sh file approach. In a terminal (keyboard: ctrl+alt+t: gedit patch_file.sh

Copy and paste the commands that would go on the terminal, to the .sh file and below the header as shown below (gedit).

#!/bin/sh
diff old.txt new.txt > patch.patch # to create the patch
patch old.txt -i patch.patch -o patched_old.text # to apply patch

Make the script executable (terminal):

chmod +x patch_file.sh

Run the script (terminal):

./patch_file.sh # may require sudo access depending on the directory affected
muru
  • 207,228
2

If you want to edit a file, use a file editor, there are command based file editors that can be used from scripts, like ex or ed.

geirha
  • 47,279
1

You're looking for sed or awk. I find sed to be simpler and awk to be more powerful.

Here's an example from another question.

sed -i 's/gedit.desktop/yournew.desktop/g' /usr/share/applications/defaults.list

This means:

  • search in file /usr/share/applications/defaults.list
  • Find/grep gedit.desktop
  • Replace with yournew.desktop
  • Apply the changes in place -i
muru
  • 207,228
RobotHumans
  • 30,112
0

Depending on what you need to edit, if you're familiar with vi then ed may be useful.

glenn jackman
  • 18,218