Results 1 to 10 of 10
How can you find and replace text in a file via CLI?...
- 09-10-2005 #1Linux Engineer
- Join Date
- Jan 2005
- Location
- Chicago (USA)
- Posts
- 1,028
CLI find and replace
How can you find and replace text in a file via CLI?
- 09-10-2005 #2
I think you should be able to use the find as well as the sed commands, but I'm not sure of the syntax, so check their man pages.
- 09-10-2005 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Er, sed does the finding before it replaces! find has its uses, but not for finding and replace text in a file (finding the files to operate on, that's another matter).
Originally Posted by dylunio
- 09-10-2005 #4Linux Engineer
- Join Date
- Jan 2005
- Location
- Chicago (USA)
- Posts
- 1,028
How would I find a regular expression ("[\d][\d]\)") and replace it with "*"?
sed -re "s/[\d][\d]\)/*/g" wiki
and
sed -re "s/^[\d][\d]\)/*/g" wiki
don't work. Nothing changes. I know the regular expression is right because I've tried it in Kate. I don't want to use Kate for everything because that would take a long time.
- 09-10-2005 #5
Now wait. Are you trying to change every place that [\d][\d]\) matches, or are you trying to replace every literal instance of [\d][\d]\)?
For example, do you want to replace:
a) "11)"
or
b) "[\d][\d]\)"
?
If a), what you have should work. If b), you'll probably need:
I haven't tested that, though.Code:sed -re "s/\[d\]\[d\]\)/*/g" wiki
DISTRO=Arch
Registered Linux User #388732
- 09-10-2005 #6Linux Engineer
- Join Date
- Jan 2005
- Location
- Chicago (USA)
- Posts
- 1,028
That didn't do anything.
I want to replace 10), 11), 12) et cetera to make a WikiMedia wiki cleaner.
- 09-10-2005 #7Linux Engineer
- Join Date
- Sep 2003
- Location
- Knoxhell, TN
- Posts
- 1,078
to use a literal asterisk in sed, you need \* not just *... * has special meaning in regex syntax (0 or more occurrences of the previous character)..
Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
- 09-10-2005 #8
Okay, I just got it to work:
I've found that sometimes \d doesn't work very well, so you have to manually type in [0-9].Code:sed -re "s/^[0-9][0-9]\)/*/g" wiki
Also, that worked on my test file. Lemme know if it doesn't work for you.
EDIT:
And lordnothing, in s/FOO/BAR, BAR is interpreted as a literal string. So you can use an asterisk there.
EDIT 2:
That command actually only prints it and doesn't replace the file.
To actually replace the file, I did:
The "-i" will edit in place instead of outputting it to the CLI.Code:sed -re "s/^[0-9][0-9]\)/*/g" wiki -i
If you want to backup the file, you could do:
Which would copy wiki to wiki.bak, then edit wiki.Code:sed -re "s/^[0-9][0-9]\)/*/g" wiki -ibak
DISTRO=Arch
Registered Linux User #388732
- 09-10-2005 #9Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Also, you don't need to escape the ) in pattern1. In fact you shouldn't because it has special meaning in sed when used in conjunction with \(.
produces the result "bert=fred".Code:echo "fred=bert" | sed 's/\([^=]*\)=\(.*\)/\2=\1/'
- 09-10-2005 #10Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Only in pattern1, not in the replacement string.
Originally Posted by lordnothing


Reply With Quote
