Download Debian Reference

Transcript
Chapter 8. Debian tips
8.6.13
118
Regular-expression substitution
Replace all instances of FROM_REGEX with TO_TEXT in all of the files FILES . . . :
$ perl -i -p -e ’s/FROM_REGEX/TO_TEXT/g;’ FILES ...
-i is for “in-place editing”, -p is for “implicit loop over FILES . . . ”. If the substitution is
complex, you can make recovery from errors easier by using the parameter -i.bak instead of
-i; this will keep each original file, adding .bak as a file extension.
8.6.14 Edit a file in place using a script
The following script will remove lines 5–10 and lines 16–20 in place.
#!/bin/bash
ed $1 <<EOF
16,20d
5,10d
w
q
EOF
Here, ed commands are the same as vi command-mode commands. Editing from the back of
file makes it easy for scripting.
8.6.15 Extract differences and merge updates for source files
Following one of these procedures will extract differences between two source files and create
unified diff files file.patch0 or file.patch1 depending on the file location:
$ diff -u file.old file.new1 > file.patch0
$ diff -u old/file new1/file > file.patch1
The diff file (alternatively called patch file) is used to send a program update. The receiving
party will apply this update to another file by:
$ patch -p0 file < file.patch0
$ patch -p1 file < file.patch1
If you have three versions of source code, you can merge them more effectively using diff3:
$ diff3 -m file.mine file.old file.yours > file