Sziasztok,
tegyük fel, hogy van pár html file, bennük pl linkek, amik megváltoztak. Szeretném az ta sort mondjuk átírni, vagy csak a kifejezést teljesen mindegy.
sed-et próbálgattam, de az csak a képernyőre adta ki nekem, magát a file-t nem bántotta.
Köszönöm!
- 1518 megtekintés
Hozzászólások
RTFM, sed -i: edit files in place
Pl csere:
sed -ie 's/ezt/erre/' file1 file2 ...
- A hozzászóláshoz be kell jelentkezni
Köszi, megvallom, beleolvastam, nem elolvastam és ez kimaradt.
- A hozzászóláshoz be kell jelentkezni
SED FAQ, 4.4 : How do I make substitutions in every file in a directory, or in a complete directory tree?
4.4.1. - Perl solution
(Yes, we know this is a FAQ file for sed, not perl, but the
solution is so simple that it has to be noted. Also, perl and
sed share a very similar syntax here.)
perl -pi.bak -e 's|foo|bar|g' filelist
For each file in the filelist, perl renames the source file to
"filename.bak"; the modified file gets the original filename.
Change '-pi.bak' to '-pi' if you don't need backup copies. (Note
the use of s||| instead of s/// here, and in the scripts below.
The vertical bars in the 's' command lets you replace '/some/path'
with '/another/path', accommodating slashes in the LHS and RHS.)
4.4.2. - Unix solution
For all files in a single directory, assuming they end with *.txt
and you have no files named "[anything].txt.bak" already, use a
shell script:
#! /bin/sh
# Source files are saved as "filename.txt.bak" in case of error
# The '&&' after cp is an additional safety feature
for file in *.txt
do
cp $file $file.bak &&
sed 's|foo|bar|g' $file.bak >$file
done
To do an entire directory tree, use the Unix utility find, like so
(thanks to Jim Dennis for this script):
#! /bin/sh
# filename: replaceall
find . -type f -name '*.txt' -print | while read i
do
sed 's|foo|bar|g' $i > $i.tmp && mv $i.tmp $i
done
This previous shell script recurses through the directory tree,
finding only files in the directory (not symbolic links, which will
be encountered by the shell command "for file in *.txt", above). To
preserve file permissions and make backup copies, use the 2-line cp
routine of the earlier script instead of "sed ... && mv ...". By
replacing the sed command 's|foo|bar|g' with something like
sed "s|$1|$2|g" ${i}.bak > $i
using double quotes instead of single quotes, the user can also
employ positional parameters on the shell script command tail, thus
reusing the script from time to time. For example,
replaceall East West
would modify all your *.txt files in the current directory.
- A hozzászóláshoz be kell jelentkezni
Hmm... ha az ember csak otthon használ Linuxot és nincs sok dolga vele, milyen egyszerű dolgok is megakaszthatják. A leírás alapján pedig ez nem egy nagy dolog.
Biztos voltam benne, hogy megvan a megoldás, csak én siklottam át felette.
- A hozzászóláshoz be kell jelentkezni