G
G
getnaked2011-08-25 11:14:38
linux
getnaked, 2011-08-25 11:14:38

Find and replace text under Linux

There are a lot of files available. Both .txt and .html - respectively, there are a bunch of hyphens and tags. Is there an easy way to find specific text and replace it with your own? Every time to write regular expressions - laziness, I want something simpler. Under Windows, @Text Replacer worked successfully, but what about under Linux?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
B
bootch, 2011-08-25
@bootch

Under Linux, there are ls, grep, sed, awk and mana for them. Ideal for your purposes.

X
xaker1, 2011-08-25
@xaker1

$ grep -rl искомый_текст /путь/до/папки/* | xargs perl -p -i -e 's/искомый_текст/заменяемый_текст/g'

, complicate the query a little by splitting it into 2 and your problem is also solved
$ grep -rl '<?' /путь/до/папки/* | xargs perl -p -i -e 's/<?/<?php/g'
$ grep -rl '<?php=' /путь/до/папки/* | xargs perl -p -i -e 's/<?php=/<?=/g'
$ grep -rl '<?phpphp' /путь/до/папки/* | xargs perl -p -i -e 's/<?phpphp/<?php/g'

The only thing - I did not understand the conditions
which are not inside lines

R
rasa, 2011-08-25
@rasa

Attach the plugin to gedit.

L
Livid, 2011-08-27
@Livid

There is no universal solution. You can draft a script in any language for this task.
I will make a reservation right away that awk, sed, perl in the most general case are not suitable. Writing a script in python or php is not difficult, I guess. Something like that. Multiline strings (sorry for the pun) are specified with triple quotes. You can read from and to from files, of course. open(filename).read() If it's really bad with programming, tell me how it's more convenient for you to set the input, I'll write a script.
#!/usr/bin/python
import sys
data=open(sys.argv[1]).read().replace("from","to")
open(sys.argv[1],"w").write(data)

H
holyorb2, 2011-08-25
@holyorb2

Yesterday I raised a similar topic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question