LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [sed] replace string? (https://www.linuxquestions.org/questions/programming-9/%5Bsed%5D-replace-string-156251/)

chuanyung 03-11-2004 07:13 AM

[sed] replace string?
 
Dear all,

I'm sorry that I can't understand when I man sed.
What I want to do are replacing some string and inserting strings in first line.

The strings I want to replace, ex:
first second; ==> //first second;
#include <FlexLexer.h> ==> #include "FlexLexer.h"

I may insert a string,such as #include "myheader.h", to first line of some file.

How should I do?
Thanks for your help.

Best Regards,
chuanyung.

mhiggins 03-11-2004 07:25 AM

Here are some examples:

One file
sed s/\<FlexLexer.h\>/\"FlexLexer.h\"/ yourfile.c >yourfile.new; mv yourfile.new yourfile.c

a few files
for i in 'file1 file2 file3'
do
sed s/\<FlexLexer.h\>/\"FlexLexer.h\"/ $i >$i.new
mv $i.new $i
done

A dir tree of files
for i in `find -name file\*`
do
sed s/\<FlexLexer.h\>/\"FlexLexer.h\"/ $i >$i.new
mv $i.new $i
done

-Matt

Not tested but should work ...

Hko 03-11-2004 10:31 AM

"info sed" is much explainig than "man sed".
If you're not familiar with "info" try readingi "info info"

Here's more sed explanation, examples, tricks....
http://www.faqs.org/faqs/editor-faq/sed/
http://main.rtfiber.com.tw/~changyj/sed/
http://sed.sourceforge.net/grabbag/

chuanyung 03-11-2004 08:42 PM

Thanks, I finished it.


All times are GMT -5. The time now is 09:26 PM.