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
  • 197,895
  • 55
  • 485
  • 740

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
  • 299,756
  • 1
    A seasoned software developer I used to work with used to say: "If you have a problem that you plan to use regular expressions to solve... Well, now you have two problems." ;-) – MrWonderful Oct 22 '20 at 21:32
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
  • 197,895
  • 55
  • 485
  • 740
  • +1 patch is always the straight way for medium to heavy or complex modification. I don't know why some try to do every thing using regex tools . – user.dz Jun 17 '16 at 14:53
  • +1 so much better than reverse engineering a regex that seems to apply the change you want on the file you test with... but when applied to other instances of that file sometimes has other effects. – bigjosh Mar 29 '19 at 15:49
  • Does a patch make sense if I want to replace all instances of specific text strings, i.e. replace all " " with "_", all "minus" with "-", and all "gb|<string_of_numbers>|ARO:" with "ARO:"? Thanks. – Josh Aug 19 '19 at 13:10
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
  • 46,101
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
  • 197,895
  • 55
  • 485
  • 740
RobotHumans
  • 29,530
0

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

glenn jackman
  • 17,900