Showing posts with label replace. Show all posts
Showing posts with label replace. Show all posts

Monday, January 6, 2014

Finding and Replacing Unicode Characters

If you'd like to find a particular multibyte character, you can do the following.
/\%u001c
You can also do a find and replace as you normally would.
%s/\%u001c//g

Wednesday, June 3, 2009

Recursively Replace Ctrl-M

Ctrl-M's are a plague inflicted upon programmers everywhere. I've mentioned a number of ways to deal with them in the past, and some readers have also contributed helpful advice as well. Today I ran into a situation where I needed to do a huge source diff on two different directory trees. My preprocessor happened to strip out all Ctrl-M leading to a bunch of false positives on files that differed. Some Google'ing around lead me to find this gem, which recursively strips Ctrl-M on all files within a given directory.


for file in $(find /path/to/dir -type f); do
tr -d '\r' <$file >temp.$$ && mv temp.$$ $file
done


In order to give credit where it's due, a user named blowtorch gave the tip at the following forum.

Thursday, April 17, 2008

Easier Search + Sub

I can't say how many times I've tried to work out a complex substitute command in Vim only to have the pattern fail and not match what I wanted. I've recently come across an easier way to accomplish a substitution on a pattern that's already guaranteed to match. Say you have the following data in your editor.

"this is a line 12345"

Execute a search (instead of a sub):
" match digits
/\d\+

Now execute a sub with no pattern:

%:s//7890/g

The result would be:

"this is a line 7890"

Obviously this is a simple example, but I find this technique handy whenever I'm working out complex patterns.

Friday, March 21, 2008

Perl Mass Substitution

Here's a nice trick I've used many many times over the years. Say that you have a group of files, and you want to replace some text in every file quickly and easily. Perl makes this really easy, and it's ubiquity pretty much guarantees it's around if you're on a *nix platform. For the sake of example, we'll pretend we have a group of html files, and we want to change every occurrence of index.html to index.php. The syntax is as follows:

[edited - thanks Chris!]

perl -pi.bak -e 's/index\.html/index.php/gi' *.html

The command line options provided break down as follows:

-p assumes a while loop over each line of every file and implicitly prints
-i specifies that you want in-place substitution on the file (no redirection of STDOUT required)
-i also takes an option argument of a backup file extension (.bak in this case)
-e tells perl to run the following code on the command line

From there, you're using standard Perl regular expressions. Don't worry if you're not a regexp guru. In the simplest form, you can simply replace one string with another.

The Perl code breaks down as follows:

s(means substitute)/string you want to match/string to replace/
(g means replace multiple instances per line)
(i means case-insensitive matching... aka ignore uppercase and lowercase differences)

Of course if you know regular expressions, you can do all kinds of fancy stuff.

Here's a nice reference.