So now i have a need to insert ' at the beginning of text and at the end in over 2000 files. The text is always the same and is always found on the first line of the files (there are over 2000 files). The text is ../../wp-blog-header.php and the finished text should look like '../../wp-blog-header.php'. How would i make a bash script to accomplish this or is it possible? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 13 November 2007 14:06, Chris Arnold wrote:
So now i have a need to insert ' at the beginning of text and at the end in over 2000 files. The text is always the same and is always found on the first line of the files (there are over 2000 files). The text is ../../wp-blog-header.php and the finished text should look like '../../wp-blog-header.php'. How would i make a bash script to accomplish this or is it possible?
It's less a BASH task than it is a sed or, perhaps, Perl one. This one's easy first go-'round: -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- targetList=( # List files, either explicitly one.php two.php foo.php bar.php # ... or by using command substitution based on "find": $( find baseDir name='*.php' ) # ... or "ls": $( ls $baseDir/*.php ) ) for target in "${targetList[@]}"; do sed \ --in-place=.bak \ -e "1s;../../wp-blog-header.php;'&';" \ "$target" done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
It's less a BASH task than it is a sed or, perhaps, Perl one. This one's easy first go-'round: -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- targetList=( # List files, either explicitly one.php two.php foo.php bar.php # ... or by using command substitution based on "find": $( find baseDir name='*.php' ) # ... or "ls": $( ls $baseDir/*.php ) ) for target in "${targetList[@]}"; do sed \ --in-place=.bak \ -e "1s;../../wp-blog-header.php;'&';" \ "$target" done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- After copying this into a .sh file and running with php files in the same directory, i get find: baseDir: No such file or directory find: name=*.php: No such file or directory ls: /*.php: No such file or directory sed: can't read one.php: No such file or directory sed: can't read two.php: No such file or directory sed: can't read foo.php: No such file or directory sed: can't read bar.php: No such file or directory So it appears none of the "find" functions seem to work. Fine that the first one, one.php and so does not work as i have 2000+ files and do not want to hard type the filenames in. But i don't understand why the 2 other "find" functions don't find the php files? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 13 November 2007 15:35, Chris Arnold wrote:
It's less a BASH task than it is a sed or, perhaps, Perl one.
This one's easy first go-'round:
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- targetList=( # List files, either explicitly one.php two.php foo.php bar.php
# ... or by using command substitution based on "find": $( find baseDir name='*.php' )
# ... or "ls": $( ls $baseDir/*.php ) )
for target in "${targetList[@]}"; do
sed \ --in-place=.bak \ -e "1s;../../wp-blog-header.php;'&';" \ "$target"
done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
After copying this into a .sh file and running with php files in the same directory, i get find: baseDir: No such file or directory find: name=*.php: No such file or directory ls: /*.php: No such file or directory
You have to fill that part in. Do you know the names of the files? If so, list them in the first section. Do you have a set of "find" criteria that will produce the target files? If so, use that section. Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that. You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
...
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
That is the whole purpose of wanting a script.....I have over 2000 files that need to have the ' inserted into the files. No way am i going to hand type the file names in. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 13 November 2007 16:05, Chris Arnold wrote:
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
How would I know? You didn't say anything about the files on which you wish to operate. Is the target set all files whose name end in ".php"? If so, then yes. If not, then no.
You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
That is the whole purpose of wanting a script.....I have over 2000 files that need to have the ' inserted into the files. No way am i going to hand type the file names in.
"Files" could be anything. Clearly, I could not write the script to select a set of files the criteria for which remain in your mind. If there's any regularity to the names of the files on which you wish the script to operate, then no, you don't need to type them. But so far, only you know those criteria so only you can write the code that will yield that list. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Chris Arnold wrote:
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
That is the whole purpose of wanting a script.....I have over 2000 files that need to have the ' inserted into the files. No way am i going to hand type the file names in.
You asked an incomplete question, so you received an incomplete answer. Where are the files (don't say "my computer")... are they all in one directory? Are there any other files in those directories which you DON'T want modified? Randall is no more of a mindreader than you are, so quit chastising him for trying to help you. I suggest you read the following sections of the "How To Ask Questions" page on Eric S. Raymond's site. (yes, I'm serious!) <http://www.catb.org/~esr/faqs/smart-questions.html#before> <http://www.catb.org/~esr/faqs/smart-questions.html#beprecise> <http://www.catb.org/~esr/faqs/smart-questions.html#explicit> <http://www.catb.org/~esr/faqs/smart-questions.html#courtesy> <http://www.catb.org/~esr/faqs/smart-questions.html#classic> <http://www.catb.org/~esr/faqs/smart-questions.html#id271954> <http://www.catb.org/~esr/faqs/smart-questions.html#examples> Oh hell, just read the whole page: <http://www.catb.org/~esr/faqs/smart-questions.html> -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Aaron Kulkis wrote:
Chris Arnold wrote:
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
That is the whole purpose of wanting a script.....I have over 2000 files that need to have the ' inserted into the files. No way am i going to hand type the file names in.
You asked an incomplete question, so you received an incomplete answer.
Where are the files (don't say "my computer")... are they all in one directory?
Are there any other files in those directories which you DON'T want modified?
Randall is no more of a mindreader than you are, so quit chastising him for trying to help you.
I suggest you read the following sections of the "How To Ask Questions" page on Eric S. Raymond's site. (yes, I'm serious!)
<http://www.catb.org/~esr/faqs/smart-questions.html#before> <http://www.catb.org/~esr/faqs/smart-questions.html#beprecise> <http://www.catb.org/~esr/faqs/smart-questions.html#explicit> <http://www.catb.org/~esr/faqs/smart-questions.html#courtesy> <http://www.catb.org/~esr/faqs/smart-questions.html#classic> <http://www.catb.org/~esr/faqs/smart-questions.html#id271954> <http://www.catb.org/~esr/faqs/smart-questions.html#examples>
Oh hell, just read the whole page:
Whatever...Aaron, you arrived a little late on the scene, so i will excuse you :) No way was i chastising Randall for his help. Quite the opposite, i thank Randall and all of you for all your help. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Chris Arnold wrote:
Aaron Kulkis wrote:
Chris Arnold wrote:
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
You didn't give any information about which files to operate on, so obviously I could not make that part of the sample / example script definite.
That is the whole purpose of wanting a script.....I have over 2000 files that need to have the ' inserted into the files. No way am i going to hand type the file names in. You asked an incomplete question, so you received an incomplete answer.
Where are the files (don't say "my computer")... are they all in one directory?
Are there any other files in those directories which you DON'T want modified?
Randall is no more of a mindreader than you are, so quit chastising him for trying to help you.
I suggest you read the following sections of the "How To Ask Questions" page on Eric S. Raymond's site. (yes, I'm serious!)
<http://www.catb.org/~esr/faqs/smart-questions.html#before> <http://www.catb.org/~esr/faqs/smart-questions.html#beprecise> <http://www.catb.org/~esr/faqs/smart-questions.html#explicit> <http://www.catb.org/~esr/faqs/smart-questions.html#courtesy> <http://www.catb.org/~esr/faqs/smart-questions.html#classic> <http://www.catb.org/~esr/faqs/smart-questions.html#id271954> <http://www.catb.org/~esr/faqs/smart-questions.html#examples>
Oh hell, just read the whole page:
Whatever...Aaron, you arrived a little late on the scene, so i will excuse you :) No way was i chastising Randall for his help. Quite the opposite, i thank Randall and all of you for all your help.
I followed the whole exchange. You need to read the page, because, if you had written your question better, you would have recieved a useful answer much more quickly, without raising animosity. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Chris Arnold schreef:
Randall R Schulz wrote:
You have to fill that part in. Do you know the names of the files? If so, list them in the first section.
Do you have a set of "find" criteria that will produce the target files? If so, use that section.
Do you have a simple shell "glob" (wild-card) expression that will yield the proper targets? If so, use that.
Shouldn't *.php yield the results i need as far as find the 2000 files?
Only if 1. All target files are in one and the same directory 2. You are in that directory 3. All target files have extension .php 4. No other files in that directory have extension .php So you have to say something about your directory structure to answer that one. -- Jos van Kan registered Linux user #152704 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 14 November 2007, Chris Arnold wrote: [snip]
After copying this into a .sh file and running with php files in the same directory, i get find: baseDir: No such file or directory find: name=*.php: No such file or directory ls: /*.php: No such file or directory sed: can't read one.php: No such file or directory sed: can't read two.php: No such file or directory sed: can't read foo.php: No such file or directory sed: can't read bar.php: No such file or directory
So it appears none of the "find" functions seem to work. Fine that the first one, one.php and so does not work as i have 2000+ files and do not want to hard type the filenames in. But i don't understand why the 2 other "find" functions don't find the php files?
First of all you did run the script unmodified, while Randall clearly mentioned in his code sample alternatives, marked by "# ... or xyz" So it is EITHER
# List files, either explicitly one.php two.php foo.php bar.php
OR
# ... or by using command substitution based on "find": $( find baseDir name='*.php' )
OR
# ... or "ls": $( ls $baseDir/*.php )
You obviously don't want the first method ;) Now you need to adapt the placeholder baseDir / $baseDir to reflect your setup. Let's use the last example, ls: ls ~/Documents/*.odt would list all *.odt files in my Documents directory (if there are any). You see, I replaced $baseDir by the actual base directory (hint hint baseDir ;) ) so change the $baseDir to your real path, and delete the other alternatives, and try again. BUT make a backup of your files first, just in case ... HTH, Matt PS. Thanks to Randall for the code, I could learn from it and use it already! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 13 November 2007 18:10, Matt T. wrote:
...
BUT make a backup of your files first, just in case ...
Presumably using the "--in-place=.bak" option makes that unnecessary. One could either back up all the target files (I'd probably tar them up) or use the in-place-with-backup option, but presumably doing both would be overkill.
HTH, Matt
PS. Thanks to Randall for the code, I could learn from it and use it already!
De nada. (But no warrantee...) RRS -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Chris Arnold wrote:
So now i have a need to insert ' at the beginning of text and at the end in over 2000 files. The text is always the same and is always found on the first line of the files (there are over 2000 files). The text is ../../wp-blog-header.php and the finished text should look like '../../wp-blog-header.php'. How would i make a bash script to accomplish this or is it possible?
I could just be tired, or your request is ambiguous. I can't determine if you just need to enclose the top line in single quotes in 2K files, or _all_ lines in 2K files. I'm assuming here your files are in the same directory. In the first instance, sed -i "1s/\(^.*$\)/'\1'/" input.txt In the second, sed -i "s/\(^.*$\)/'\1'/" input.txt You just want to change the input.txt to a bash for loop which catches all the relevant files. JA - -- http://www.DonAssad.com jabber ID: josef.assad@gmail.com Please consider the environment; do you really need to print out this e-mail? -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFHOiYsFcf72sjD2+QRAnJ4AJ4u8516are7kHWmWkzPjZeNrY1BEwCcDCAD ew/iAhxytdKCYtKkjyoG6f0= =oIXY -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tue, Nov 13, 2007 at 05:06:23PM -0500, Chris Arnold wrote:
So now i have a need to insert ' at the beginning of text and at the end in over 2000 files. The text is always the same and is always found on the first line of the files (there are over 2000 files). The text is ../../wp-blog-header.php and the finished text should look like '../../wp-blog-header.php'. How would i make a bash script to accomplish this or is it possible?
For 2000 files the limit of the `*' expansion could be reached. Therefore you may use `find' together with a shell loop and the `ed' ... e.g. find <target_dir>/ -name '*.php' | \ while read php; do ed $php &> /dev/null <<-EOF 1 ,s/.*/'&'/ . w q EOF # ^^^ this is a <TAB>! done if you want to use shell special signs literal like `$' in your text you may use `"EOF"' instead of `EOF'. Please note that if you're using a `-' before the `EOF' or before `"EOF"' you are able to indent the second `EOF'/`"EOF"' with a tabulator sign. If the `-' is missed the `EOF'/`"EOF"' has to be placed at the very first beginning of the line. An other editor is the sed, with its inplace option `-i': find <target_dir>/ -name '*.php' | \ while read php; do sed -ri "1,1 s/.*/'&'/" $php done it is a bit shorter ... but ed is in some cases more powerfull. Nevertheless both editors allow to use other boundary characters as the `/' ... sometimes I use `s@old@new@' or `s!old!new!'. Werner -- "Having a smoking section in a restaurant is like having a peeing section in a swimming pool." -- Edward Burr -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (7)
-
Aaron Kulkis
-
Chris Arnold
-
Dr. Werner Fink
-
Jos van Kan
-
Josef Assad
-
Matt T.
-
Randall R Schulz