How to programmatically edit a file using only terminal?
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....
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
- Text Manipulation with sed from linuxjournal
- sed one-liners from linuxhowtos
- Beginner's guide to sed
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
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.