Re: [SLUG] awk sed (grep?)
On Sat, 27 May 2006 12:31 pm, Peter Chubb wrote: Or use ed: for i in * do ed - $i \EOF 1,$s/str/repl/g w q EOF done Sweet - old school ed and here document. Very nice:) James -- Most people eat as though they were fattening themselves for market. -- E.W. Howe pgpEon1WHkBKY.pgp Description: PGP signature -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
john hedge wrote: My challenge is to search a directory of text files for a string which I want to replace wherever it occurs with a new string and save the file with the changes in the same original file name. In the past I've used something like: for file in `find -type f -name 'whatever'` do (echo '1,$s/oldstring/newstring/g' echo 'wq') | ex $file done .. quite successfully for this purpose. It basically just drives 'ex' to do precisely what you'd do if you were doing it manually. Test it somewhere with a copy of your files first though! regards Terry -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
Peter Chubb wrote: Or use ed: for i in * do ed - $i \EOF 1,$s/str/repl/g w q EOF done 'HERE' file, nice. That's more elegant than my suggestion. Terry -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
[SLUG] awk sed (grep?)
Hi,I've been hitting my head all day over awk and sed (grep?)!My challenge is to search a directory of text files for a string which I want to replace wherever it occurs with a new string and save the file with the changes in the same original file name. Any help is much appreciated.John -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 2006.05.26 18:37 john hedge wrote: I've been hitting my head all day over awk and sed (grep?)! My challenge is to search a directory of text files for a string which I want to replace wherever it occurs with a new string and save the file with the changes in the same original file name. Try a loop using grep with the -q option to locate the files with the requisite text then sed with the -i option to make the change. Something like: cd /path/to/directory/ for FILE in do if grep -q text to be replaced $FILE then sed -i 's/text to be replaced/replacement text/g' $FILE fi done Notes: 1. grep -q does not need to be in [test brackets] because it is a test in its own right 2. if you have filenames with spaces or funny characters it serves yourself right. HTH, Robert Thorsby -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 26/05/2006, at 6:50 PM, Robert Thorsby wrote: On 2006.05.26 18:37 john hedge wrote: I've been hitting my head all day over awk and sed (grep?)! My challenge is to search a directory of text files for a string which I want to replace wherever it occurs with a new string and save the file with the changes in the same original file name. Try a loop using grep with the -q option to locate the files with the requisite text then sed with the -i option to make the change. Something like: cd /path/to/directory/ for FILE in do if grep -q text to be replaced $FILE then sed -i 's/text to be replaced/replacement text/g' $FILE fi done Notes: 1. grep -q does not need to be in [test brackets] because it is a test in its own right 2. if you have filenames with spaces or funny characters it serves yourself right. 3. In case you want this to be portable, keep in mind that '-i' is a nonstandard extension. '-i' takes an argument, on my platform at least. -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 2006.05.26 21:47 Sam Lawrance wrote: On 26/05/2006, at 6:50 PM, Robert Thorsby wrote: cd /path/to/directory/ for FILE in do if grep -q text to be replaced $FILE then sed -i 's/text to be replaced/replacement text/g' $FILE fi done Notes: 1. grep -q does not need to be in [test brackets] because it is a test in its own right 2. if you have filenames with spaces or funny characters it serves yourself right. 3. In case you want this to be portable, keep in mind that '-i' is a nonstandard extension. '-i' takes an argument, on my platform at least. The argument for -i is optional; if supplied sed provides a backup; if not supplied sed amends the input file. See man sed Robert Thorsby -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 26/05/2006, at 10:14 PM, Robert Thorsby wrote: On 2006.05.26 21:47 Sam Lawrance wrote: On 26/05/2006, at 6:50 PM, Robert Thorsby wrote: cd /path/to/directory/ for FILE in do if grep -q text to be replaced $FILE then sed -i 's/text to be replaced/replacement text/g' $FILE fi done Notes: 1. grep -q does not need to be in [test brackets] because it is a test in its own right 2. if you have filenames with spaces or funny characters it serves yourself right. 3. In case you want this to be portable, keep in mind that '-i' is a nonstandard extension. '-i' takes an argument, on my platform at least. The argument for -i is optional; if supplied sed provides a backup; if not supplied sed amends the input file. See man sed That is ridiculous. Unless you know in advance what the argument will be, it is not possible to detect that it has not been supplied. Anyhow, my main point is that any form of '-i' is a nonstandard extension. -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 2006.05.26 22:34 Sam Lawrance wrote: The argument for -i is optional; if supplied sed provides a backup; if not supplied sed amends the input file. See man sed That is ridiculous. Unless you know in advance what the argument will be, it is not possible to detect that it has not been supplied. Look, I don't know whether it is ridiculous or not. I use sed -i simpliciter often from the command line to wipe all text from a file: sed -i 'd' input.file It works for me; YMMV. I do, however, suggest you RTFM. The man page is quite clear Anyhow, my main point is that any form of '-i' is a nonstandard extension. In the context of the request I did not think that portability was an issue. If it were, then clearly something more than my QD command one-liner might be desirable. Robert Thorsby -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
On 26/05/2006, at 10:43 PM, Robert Thorsby wrote: On 2006.05.26 22:34 Sam Lawrance wrote: The argument for -i is optional; if supplied sed provides a backup; if not supplied sed amends the input file. See man sed That is ridiculous. Unless you know in advance what the argument will be, it is not possible to detect that it has not been supplied. Look, I don't know whether it is ridiculous or not. I use sed -i simpliciter often from the command line to wipe all text from a file: sed -i 'd' input.file I apologise, that wasn't called for. I think I see what version you are using - it seems that you can't have a space between -i and its argument. It's behaviour I have not seen before (SUS suggests that it is non-portable, too :). It works for me; YMMV. I do, however, suggest you RTFM. The man page is quite clear I did. It is. My man page is not your man page ;-) Anyhow, my main point is that any form of '-i' is a nonstandard extension. In the context of the request I did not think that portability was an issue. If it were, then clearly something more than my QD command one-liner might be desirable. Portability is never an issue. Until you try to run your stuff on some other platform. Take it from someone who likes to patch the linuxisms out of software for fun. -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
Thank you Robert, Matthew Sam for your ideas.It's late on a Friday night so forgive me but I'm not going to try your suggestions until tomorrow.BWJohnOn 5/26/06, Sam Lawrance [EMAIL PROTECTED] wrote: On 26/05/2006, at 10:43 PM, Robert Thorsby wrote: On 2006.05.26 22:34 Sam Lawrance wrote: The argument for -i is optional; if supplied sed provides a backup; if not supplied sed amends the input file. See man sed That is ridiculous.Unless you know in advance what the argument will be, it is not possible to detect that it has not been supplied. Look, I don't know whether it is ridiculous or not. I use sed -i simpliciter often from the command line to wipe all text from a file: sed -i 'd' input.fileI apologise, that wasn't called for.I think I see what version youare using - it seems that you can't have a space between -i and its argument.It's behaviour I have not seen before (SUS suggests thatit is non-portable, too :). It works for me; YMMV. I do, however, suggest you RTFM. The man page is quite clearI did.It is.My man page is not your man page ;-) Anyhow, my main point is that any form of '-i' is a nonstandard extension. In the context of the request I did not think that portability was an issue. If it were, then clearly something more than my QD command one-liner might be desirable.Portability is never an issue.Until you try to run your stuff onsome other platform.Take it from someone who likes to patch thelinuxisms out of software for fun. --SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] awk sed (grep?)
Robert == Robert Thorsby [EMAIL PROTECTED] writes: Robert On 2006.05.26 18:37 john hedge wrote: I've been hitting my head all day over awk and sed (grep?)! My challenge is to search a directory of text files for a string which I want to replace wherever it occurs with a new string and save the file with the changes in the same original file name. Robert Try a loop using grep with the -q option to locate the files Robert with the requisite text then sed with the -i option to make Robert the change. Something like: Robert cd /path/to/directory/ for FILE in do if grep -q text to be Robert replaced $FILE then sed -i 's/text to be Robert replaced/replacement text/g' $FILE fi done Alternatively, and more brute force, do: for i in * do sed 's/string/repl/g' $i $i.backup mv $i.backup $i done This of course assumes there are no files called x.backup in the directory. Or use ed: for i in * do ed - $i \EOF 1,$s/str/repl/g w q EOF done -- Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au http://www.ertos.nicta.com.au ERTOS within National ICT Australia -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html