hi, I'm using 3.8, and I hate to bother, but I have spent two days on the
net trying to find the answer to this problem.
I am using 'find' to batch file a sed search and replace. Sed, of course,
outputs to stdout, the problem I am having is finding the correct syntax so
that I can change the extension of the input file to create the new output
file. For example:
so if the input file happens to be smith.htm, then '{}' would be smith.htm
and the idea is that the output filename for the sed command would create a
new output file called smith.htm.new.
this of course isn't working.
any help please? thanks.
david
incomex@hotmail.com
_________________________________________________________________
PC Magazines 2007 editors choice for best Web mailaward-winning Windows
Live Hotmail.
http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_m...you pass {} to the shell as part of the filename to redirect the
output from find(1) to, it is not seen by find(1) at all (and you
don't pass the filename to sed, either).
if you want to redirect via -exec you can do it like this:
$ find . -name '*.htm' -exec sh -c 'sed s/old/new/ < {} > {}.new' \;
but do you know about the simpler perl options for in-place editing?
see perlrun(1) about the -i option, here are the more common forms:
$ perl -pi.orig -e 's,foo,bar,' *.htm
(with backup)
$ perl -pi -e 's,foo,bar,' *.htm
(without)> Find . -name "*.htm" -exec 'sed s/old/new/' > '{}'.new
the above command is probably a sytnax error, due to unterminated
-exec (add \; at the end to fix this), that apart that command should
look for a command 'sed s/old/new/' (note: it should NOT invoke sed
command with s/old/new/ argument).
find . -name "*.htm" -exec echo '{}.new' \;
should do what you want.
--
almirI'd probably use a for loop. *untested* for i in *.html ; do mv $i `echo $i | sed 's/\(.*\.\)html/\1html.new/'` ; done --Bryan
Don't use for loops with find results, they do not scale well. Also, beware of spaces in file. For this kind of thing, I generally use 'while read' find . -type f -name \*.htm -print|while read f; do sed s/old/new <"$f" >"$f.new"; done
Use xargs(1) Best Martin
Hello! For that case, it doesn't really help, because the filename is needed in a redirection. And if you need the filename in more arguments, xargs is a bit silly too, because it *has* the option -I, but that has the completely arbitrary limitation of 255 characters which can't be changed Kind regards, Hannah.
Hello!
This isn't safe wrt newlines in file names, either.
A completely safe solution would be writing a small script:
#! /bin/sh
exec sed s/old/new/ < "$1" > "$1".new
and using find . -type f -name \*.htm -exec /path/to/script {} \;
or find . -type f -name \*.htm -print0 | xargs -0 -L 1 -r /path/to/script
Kind regards,
Hannah.newlines in file names are a pretty infrequent occurrence.
In case this could happen, I would do it in perl and get rid of the sed
entirely.
#! /usr/bin/perl
use File::Find;
sub
transform
{
my $filename = shift;
open my $fh, '<', $filename or
die "Problem with $filename: $!\n";
open my $fh2, '>', "$filename.new" or
die "Problem with $filename.new: $!\n";
while (<$fh>) {
s/old/new/;
print $fh2 $_
}
}
find(sub {
return unless -f $_;
return unless m/\.htm$/;
transform($_);
}, '.');
or something like this...I think this is also correct:
find . -name '*.htm' -exec cp '{}' '{}'.new \; \
-exec sed -i s/old/new/ '{}'.new \;Hi! I don't see any -i option documented in the sed manpage. Kind regards, Hannah.
I believe the -i option is only available in gnu sed. It stands for "in-place" which means it modifies the original file. You can specify an extension for a backup file to preserve the original content. Terry
Hello! May be. But this is misc@*OpenBSD* and not misc@some-system-where-sed-means-gnu-sed Kind regards, Hannah.
...-exec sh -c 'something with $1' {} \; is fully safe as well.
--
almirHello! sh -c 'echo foo"$1"bar' baz -> foobar Seems not. Kind regards, Hannah.
a typo, sorry, it should be sh -c 'echo foo$1bar' -- baz i am cheating tho, and have sh symlinked to bash. -- almir
Hi! This works indeed. But better use the additional quotes around $1. Just Why? Better don't do that. Kind regards, Hannah.
i learnt to use bash, and posix sh is not good enaugh any more. tho to be fair, most of the features i like in bash are (probably) implemented in ksh and zsh as well, i just never bothered to learn -i on some seds (gsed, ssed, FBSD sed, maybe others) means ''in safe temp file, so doesn't overwrite any file accidentally). but it doesn't exists in OBSD sed, so his answer was 'wrong'. -- almir
Hello! Proactive security also means using safe patterns not only when you can I have never missed a shell feature unless I would use awk/perl/... It is. I'd guess it isn't in POSIX (or Single Unix) sed either, so if you give any damn on portability, I wouldn't use it. And then, mv doesn't know a -g flag. Kind regards, Hannah.
What is mv -g ? doesn't seem to be any standard option I know about ?
me feels like an idiot :/, i meant mv -f and not mv -g :/ -- almir
Here's a solution with sh+find+sed: for file in $(find . -name "*.htm"); do sed 's/old/new/' $file > $file.new; done -- Antti Harri
| Greg KH | [GIT PATCH] driver core patches against 2.6.24 |
| Greg KH | [patch 00/04] RFC: Staging tree (drivers/staging) |
| James Bottomley | Re: Integration of SCST in the mainstream Linux kernel |
| Steven Rostedt | [RFC PATCH 1/3] Unified trace buffer |
git: | |
| Jon Smirl | ! [rejected] master -> master (non-fast forward) |
| Marco Costalba | [ANNOUNCE] qgit4 aka qgit ported to Windows |
| Andi Kleen | Re: [kernel.org users] [RFD] On deprecating "git-foo" for builtins |
| Sverre Rabbelier | Git vs Monotone |
| Richard Stallman | Real men don't attack straw men |
| GVG GVG | ssh_exchange_identification: Connection closed by remote host |
| Damian Gerow | Oddly high load average |
| Benjamin Adams | BSD Port from OpenJDK |
| Michael Grollman | Re: 8169 Intermittent ifup Failure Issue With RTL8102E Chipset in Intel's New D945... |
| Volker Armin Hemmann | build error with 2.6.27.6+reiser4+ehci-hub patch. ERROR: "mii_ethtool_gset" [drive... |
| Evgeniy Polyakov | [resend take 2 0/4] Distributed storage. |
| Wenji Wu | A Linux TCP SACK Question |
| serial driver xmit problem | 54 minutes ago | Linux kernel |
| Why Windows is better than Linux | 54 minutes ago | Linux general |
| How can I see my kernel messages in vt12? | 7 hours ago | Linux kernel |
| Grub | 19 hours ago | Linux general |
| vmalloc_fault handling in x86_64 | 1 day ago | Linux kernel |
| epoll_wait()ing on epoll FD | 1 day ago | Linux kernel |
| Framebuffer in x86_64 causes problems to multiseat | 1 day ago | Linux kernel |
| Difference between 2.4 and 2.6 regarding thread creation | 1 day ago | Linux general |
| Netfilter kernel module | 1 day ago | Linux kernel |
| Compiling gfs2 on kernel 2.6.27 | 1 day ago | Linux kernel |
