Sunday, July 24, 2011

FIND and REPLACE TEXT inside a file without using TEXT Editor


FIND and REPLACE TEXT inside a file without using TEXT Editor


You can perform a text replace within a file without using VI or other text editor available on your Unix or Linux box. Below command will help you perform that action. 


Using SED:
sed -i 's/FIND TEXT/REPLACE TEXT/g' FILENAME.TXT


Using PERL:
perl -p -i -e "s/FIND TEXT/REPLACE TEXT/g" FILENAME.TXT




Where:
FIND TEXT - Content you want to replace
REPLACE TEXT - New Value of the content
FILENAME.TXT - Filename

4 comments:

Arun Rahul said...

Thanks for the Perl command dude.
For sed -i option was troubling.

kdp said...

Thanks for the efforts on gathering useful content and sharing here. You can find more question and answers on Unix and Linux in the following forum.

Unix and Linux technical discussion forum

אהרן יפה said...

Hi! Thanks for your post. I have a question:
I'm a student and i have task to replace all instances of "abc" by uppercase.
When I'm using sed command i'm getting just a visually output, and it isn't acctually replacement of the my file's content.
This is the output:
> cat f1
abc Thank You!!!
> sed 's/abc/ABC/g' < f1
ABC Thank You!!!
> cat f1
abc Thank You!!!
And when i'm using this command the file become empty:
sed 's/abc/ABC/g' < f1 >! f1
Sorry about my english and thank you very much,
Roni

vasudha dharani said...

Perl online training|Perl training|call us+919000444287 ...
www.21cssindia.com/courses/perl-online-training-36.html
Perl Online Training, Perl Scripting online training by real time Experts from Hyderabad, India. Call 9000444287 for online training demo. Online Perl training ...