[ale] sed/awk/perl whatever question

Ed Cashin ecashin at noserose.net
Tue Nov 3 16:17:46 EST 2009


On Tue, Nov 3, 2009 at 2:40 PM, Asher Vilensky <ashervilensky at gmail.com> wrote:
> I think that somebody asked the list a similar question recently, but I
> couldn't find it.  Here's the deal:
>
> I have two files:
> file1 - contains unique strings:
>
> 123abc
> 234xyz
> 123456
> kljdhs
> etc
>
> file2 is a huge log file with the unique numbers from file1 appearing in it
> multiple times.  I need a script that for each unique string in file1, will
> replace it in file2 with XXXX.  I did this on command line, but it didn't
> work:
>
> for i in `cat file`
> do
> sed -e 's/'"$i"'/XXXX/g' file2 >> file3
> done
>
>
> Or something like that.  The problem is that $i was not interpreted right.
> Any advice anybody?  I'll take it in sed/awk/perl.

One interesting thing about this problem is that the strings in file1
are not regular expressions and shouldn't be treated as such, but
the easiest ways to solve this problem in sed/awk/perl involve using
them as regular expressions.  If they're really strings like "xyz123",
then that's not a problem, but if they're allowed to be more general,
they could contain characters that have meaning to the regular
expression parser.

For example, if "a[b]c" is in file1 and you want to replace "a[b]c",
square brackets and all, in file2 with "XXXX", then it won't work.
And it will replace "abc" mistakenly.

There are awkward ways to replace literal substrings, but it's a
bit odd that it's not easier.  Python does make it easy with a string
method called "replace".

-- 
  Ed Cashin <ecashin at noserose.net>
  http://noserose.net/e/
  http://www.coraid.com/



More information about the Ale mailing list