Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and replace in the same file

Status
Not open for further replies.

whygh

Programmer
Apr 18, 2005
16
CA
Hi guys,

I am learning how to use regular expression. Can I directly find a matching string and replace it in the same file?I mean input and outout is the same file.

For example:
test.txt :
Hello
All
Sed
Awk
wall
google

after I use "s/(.){2,}\1.*//g",test.txt turn into
heo
A
sed
Awk
wa
gle
 
not really, the common ways to do that are to read the file into memory and then overwrite it as you loop through, or to write to a second file as you read the first, and then just delete/rename when you're done.

Alternatively, have you looked at the -i switch to the perl interpreter itself in perlrun?

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top