General discussion
-
CreatorTopic
-
January 31, 2000 at 6:59 pm #2082923
Scan for files and replace string?
Lockedby resch · about 23 years, 4 months ago
What is a foolproof way to scan a directory structure for files containing certain text strings and
1. produce a list of all the files containing the string and/or
2. replace the string with another stringThe script (or whatever solution is available) should ignore links made with “ln -s” (I’m aware of grep and find, but find tells me that there are too many nested links).
Must work on FreeBSD.
Topic is locked -
CreatorTopic
All Comments
-
AuthorReplies
-
-
February 2, 2000 at 2:24 pm #3897539
Scan for files and replace string?
by trace · about 23 years, 4 months ago
In reply to Scan for files and replace string?
you can use “find” command like follows:
find /dir -name “*str*” ! -type l -print-
September 20, 2000 at 7:02 pm #3742119
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
February 11, 2000 at 3:23 am #3897278
Scan for files and replace string?
by zaruba · about 23 years, 3 months ago
In reply to Scan for files and replace string?
find /DIR \! -type l -exec grep -l STRING \;
-
September 20, 2000 at 7:02 pm #3742120
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
February 15, 2000 at 8:25 am #3897890
Scan for files and replace string?
by gordon · about 23 years, 3 months ago
In reply to Scan for files and replace string?
This should work, if I’ve interpreted it right: ls -l | grep -v “^l” | grep “string”
ls -l “long” directory listing with
“-rwxrwxrwx” as first “field”
grep -v “^l” will take pipe input and exclude (-v) all items that have an “l” (Lowercase L) as the first position
grep “string” will take the results of the second pipe and look for “string”.To replace with another string, be very careful, b/c if the other string is malformed, you may mess up the wrong thing (Voice of exp. here!) Ok, this is more difficult (This is shell script):
#!/bin/kshls -l | grep -v “^l” | grep “string” | awk ‘{print $9}’ > tempfile
# Awk stmt to print filename only.
sed ‘s/string/replace/’ tempfile > tempfile1
# sed to create replacement filename.
for modfilename in `grep “^” tempfile1`
do
for filename in `grep “^” tempfile`
do
echo $filename $modfilename
# replace with:
# mv $filename $modfilename
#when you’re confident this will work.
done
done
Goes w/o saying to teston dummy files fi-
September 20, 2000 at 7:02 pm #3742121
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
March 22, 2000 at 7:18 am #3901691
Scan for files and replace string?
by ray · about 23 years, 2 months ago
In reply to Scan for files and replace string?
I would go along with answer #2 with a few changes. I think something like this would provide fool proof results:
find /DIR -type f -exec grep -l STRING {} \;
also add a “-i” to the grep if you want it to be case insensitive.
as for the replacement of the string I would use a heredoc and “ex”
-
September 20, 2000 at 7:02 pm #3742122
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
April 3, 2000 at 4:53 am #3896273
Scan for files and replace string?
by david_totsch · about 23 years, 2 months ago
In reply to Scan for files and replace string?
I don’t have access to FreeBSD, but read the man(1) page on find(1). You should be able to select based on the “type” of the file. The first inclination would be to avoid sybolic links:
find . ! -type l
But, you could wind up doing somethingnasty like running grep(1) against a named pipe. Use positive logic looking for “regular files”:
find . -type f
This will prevent you from wasting time grepping directories, character files and the like.
Now, you want to form a list of files that contain the string you are looking for. You have two choices:
1) find . -type f -exec grep “string” {} \;
or
2) find . -type f | xargs grep “string”
Number one will spaw a grep for every file found by find(1). Spawing a new process is one of the most expensive UNIX operations. So, you might consider number two. The second option uses xargs to spawn fewer copies of grep, however, you may bump up againt an arguments length limit.
Either way, grep is going to return lines that contain the fi
-
September 20, 2000 at 7:02 pm #3742123
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
June 5, 2000 at 10:18 am #3894176
Scan for files and replace string?
by slater · about 22 years, 12 months ago
In reply to Scan for files and replace string?
I will assume your directory does not have subdirectories. Even if it did, you can use this process on each subdirectory. I would do this in steps. (If you use one command then it can get real nasty and confusing.)First determine which files havethe string you wish to replace:
cd dir_to_search
ls -1 | xargs grep {string_to_search} | awk -F : ‘{print $1}’It looks confusing but the output is very neat. This will list only the files that contain the string. This will cover your 1st question. As far as the second question goes (replace the string) I would use sed in a shell script.
I do not have the time to write the script for you but the basic sed command to use in a script is:
sed ‘s/string_searched_for/string_to replace_with/g’
You can redirect the output from the command ls -1 command above to a file (CONTROL_FILE) then use that file as a control file for a while loop. The script would look soimething like this:
cat $CONTROL_FILE | while read TEXT
do set ${TEXT}
e-
September 20, 2000 at 7:02 pm #3742124
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
The question was auto-closed by TechRepublic
-
-
September 20, 2000 at 7:02 pm #3742118
Scan for files and replace string?
by resch · about 22 years, 8 months ago
In reply to Scan for files and replace string?
This question was auto closed due to inactivity
-
-
AuthorReplies