Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 28 Jan 2005 17:54:06 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        freebsd-questions@freebsd.org
Subject:   Re: One-line global string replace in all files with sed (or awk?)
Message-ID:  <20050128155406.GA659@orion.daedalusnetworks.priv>
In-Reply-To: <1802109563.20050128120437@wanadoo.fr>
References:  <1098984237.20050128065616@wanadoo.fr> <20050128064057.GB1559@gothmog.gr> <1802109563.20050128120437@wanadoo.fr>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-01-28 12:04, Anthony Atkielski <atkielski.anthony@wanadoo.fr> wrote:
>Giorgos Keramidas writes:
>> grep will do.  You just have to pass it the right option:
>>
>>         find . -type f | xargs grep -l 'foo' | \
>>             xargs sed -i '' -e 's/foo/bar/g'
>>
>> When passed the -l option (this is a lowercase 'EL'), it will not print
>> the matched lines.  Only the name of the files that *do* match. Then,
>> once you have a list of files that really do match with 'foo' as a
>> pattern, you can xargs sed on the list to substitute whatever you want :-)
>
> It's interesting that there are multiple ways to do this in FreeBSD,
> whereas I've never found a way to do it in Windows, even with the fancy
> Visual InterDev I've been using for several years (it will _search_ for
> strings in multiple files, but it won't do replacements, so you have to
> search and then edit each file by hand--try doing this several hundred
> times, several times in a row sometime!).

I honestly feel pity for the Windows using friends I have in cases like
this.

A typical example is when they start writing "File Renamers", to mass
rename collections of files (i.e. mp3 audio) and start being bitten by
bugs in the renamer itself.

Another typical example is what I call the robo-user.  A person who
starts training his fingers and/or brain to repeatedly hit the same
sequence of keys in the keyboard, in an effort to rename dozens of files
within Explorer.

Writing shell scripts to do the job is not necessarily easier, since you
have to learn the tools that are available and learn all about the ways
they can be glued together, before becoming *really* productive.  It
pays a thousand times back though, when you start doing in a couple of
lines what would take hours of error-prone, manual labor.

This is why I have been heard saying:

"Unix is for lazy people.  So lazy, in fact, that they start learning
languages and tricks to avoid as much work as possible, by making their
computers do it."



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050128155406.GA659>