search and replace string with special character perl or sed.
I am trying to use perl or sed to edit some directory paths in configuration files. The problem is I want to delete part of the path (so replace BOBDOG/ with nothing). Following is some of the code I have tried.
This leaves 0 byte files behind :( Code:
#!/bin/bash Code:
#!/bin/bash Any ideas? |
Sed can use many different delimiters for substitutions. Pipes for example. This is particularly handy for working with paths, and other code.
for example. Code:
echo '/home/bobdog/billdog/documents' | sed "s|bobdog/||g" Quote:
It is better (and easier) to let BASH expand * itself.. Code:
for file in * ; do |
It would help a lot if you would provide an actual example of the input that needs to be worked on, and the desired output.
Code:
perl -e "s/"$strFind"/""/g;" -pi $(find test -type f); 2) The proper way to use find with external tools is through the -exec option. Also, note that the syntax is "find <directories> <tests> <action>" Code:
find . -type f -exec perl -e "s/$strFind/\"\"/g;" -pi '{}' \+ http://mywiki.wooledge.org/UsingFind http://www.grymoire.com/Unix/Find.html Code:
SCRIPTNAME=`echo $0|cut -f2 -d/` Code:
scriptname=${0##*/} 3) $(..) is highly recommended over `..` 4) Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them. Code:
if [ "$file" <> "$SCRIPTNAME" ]; then http://mywiki.wooledge.org/ArithmeticExpression http://mywiki.wooledge.org/BashFAQ/031 http://wiki.bash-hackers.org/commands/classictest http://wiki.bash-hackers.org/syntax/...nal_expression 2) "<>" is a file redirection, not a comparison test. In bash at least, it opens up the filename following it for both reading and writing, and the parameter in front of it should be the number of the file descriptor. To compare two strings, use "=" in the traditional test, or "==" in the double-bracket test. This is also another reason not to use "[..]". Since it's just a simple command, the redirector is allowed to be processed first, and the file will be created if it doesn't exist. But inside the "[[..]]" keyword, it's considered improper syntax instead, generating an error. 3) A better way to exclude the script name from the loop is with the -ef comparison anyway. Code:
if [[ ! $file -ef $0 ]] ; then Code:
sed "s|$strFind|\"\"|g" $file > temp; mv temp $file; http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes Refer back to my very first point as well. 2) If you want to replace a string with nothing, as you stated, don't use quotes in the RHS replacement. Just leave it empty. Of course if you do want the quotes to be inserted, it's just fine. gnu sed has "-i" as an option for editing a file in place, letting you skip the tempfile. Code:
sed -i "s|$strFind||g" "$file" |
More detailed information.
First thanks for the replies. I have a lot to go through. Second, I will give some more information about what I am trying to do.
I need to loop through a number of directories and sub-directories looking for a particular string (in a path) in various files and deleting it. A typical file might look like this. Code:
#!/bin/sh I was able to get this working in a single directory using the sed commands as you specified, David, but I would need to move the script to every directory and there are just too many, leading me to using the *find* which is recursive. Let me know if anything else would be helpful in understanding the problem. Thanks again for your help. |
Missed a line
Sorry I forgot to give you the new find/perl command I was trying to use.
Code:
#!/bin/bash This gives me *Illegal division by zero at -e line 1, <> line 1.* and it doesn't change any of the files. Thanks, Kevin |
All times are GMT -5. The time now is 04:23 PM. |