Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
After hours (literally) of searching the web and reading man pages, I think I've come up with the following:
Code:
find . -exec grep 'path/to/file' -print | xargs -0 -I new_path mv {this is where I get confused}
So my code above is incomplete, obviously. In order to finish replacing the string, I need to mv the new file into the old file's spot. How do I do this, by incorporating it into my line of code?
And do I need to modify my code at all? Any thoughts, help, or suggestions would be appreciated.
Sed has a switch for editing files in place, its -i. This allows it to run the arguments on the file and make changes without having to create a copy and then later move the copy to the original. It also uses the -f switch to use commands from a file to be executed. A single sed command to replace one instance of text with another would look like this: sed -i 's:{target}:{new}:g' the :g tells sed to continue on replacing the first expression with the second until the end of file is reached, otherwise it would stop after the first occurrence. If you use multiple commands you would use -e before each one to let sed know that it's a new argument, otherwise it would treat it as part of the first argument.
ahh, the -i flag! Why did I not see that in my man page... oh well. I see it in there now.
So, when do the following, I get an error:
Code:
find . -exec grep 'path/to/file' -print | sed -i 's:/path/to/file:new/path/to/file:g'
The error is...
Code:
$ sed: no input files
$ find: missing argument to `-exec'
The command I am trying to execute is "grep" so why isn't that being translated by find as a command? Or am I doing something wrong? (obviously, I am, but what?)
For the sed part, you have to tell it what file is being altered after the arguments. It looks like you are trying to rename files though? If that's the case you will want to use the rename command instead. You wouldn't have to use grep to do this, using the example from the rename man page you would change the file extensions of any file with .htm to .html like this:
rename .htm .html *.htm
Ok in that case you would simply do the following:
sed -i 's:y:z:g' x
To explain that line piece by piece:
-i for editing in place
s: means you are replacing text
y is the expression being changed
z is the replacement for y
g means global, continue until end
x is the file being altered.
If you are doing this on every file in a directory, and there are no hidden files, you can use * instead of a single filename. That will run the command on EVERY file, no exceptions. So only do that if you are positive it's what you want. If there are multiple files that you want to change, but they don't share a common naming scheme or are scattered about you can make this into a function so that you just type the function name followed by the filename. You would add the function to your ~/.bashrc like so:
function myscriptname {
FILE=$1 # where the first argument given after myscriptname is file
sed -i 's:y:z:g' $FILE
}
Then you could just type the following in your shell 'myscriptname file. This is also useful in that if you have multiple expressions you want replaced in the same file you simply add another sed command directly under the original, or any other types of commands you want done to that file.
I was trying to make things way too complicated here...
Sometimes I wonder if that's my raison d'etre.. It's very easy to over-complicate things (I know this from first-hand experience, but seem to need to relearn it regularly) by assuming something involves much more than it actually might. LOL.
I haven't got my glasses on at this moment, but at a glance: since you're using a period as the sed special character, you probably need to escape the periods within the LHS and RHS of the expression. (such as .jpg should be \.jpg)
If this isn't it, I'll have another look after if nobody else has offered advice.
PS - use a special character that does not appear in the LHS or RHS,such as maybe a % sign, or @ sign.
Sasha
Last edited by GrapefruiTgirl; 01-15-2010 at 03:08 PM.
I've been looking at this thread, now say I wanna do the same thing as Rudy only I want to do it recursively on all of the files in a certain directory.
Here is what I've got so far:
Code:
find . "*.js" -exec sed -i s/jstestdriver\.console\.log.*;//g ;
I've been looking at this thread, now say I wanna do the same thing as Rudy only I want to do it recursively on all of the files in a certain directory.
Here is what I've got so far:
Code:
find . "*.js" -exec sed -i s/jstestdriver\.console\.log.*;//g ;
But then I get the error:
Code:
sed: no input files
You need find's special symbol "{}" (without the quotes) which find replaces with the found file name and you need to escape the ";" or it is seen by the shell as a command terminator
Code:
find . "*.js" -exec sed -i s/jstestdriver\.console\.log.*;//g {} \;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.