View Single Post
  #7   (View Single Post)  
Old 6th December 2008
drl's Avatar
drl drl is offline
Port Guard
 
Join Date: May 2008
Posts: 19
Default

Hi.

Interesting question. There is an fgrep, so we might wonder why there is no fsed. Rather than escape all the special characters, if we could use features that did not involve regular expressions, we'd be set.

As it turns out, perl function index does a plain string search, and substr allows replacement.

I put together a perl script -- sfs -- to do just that. Here is the brief help:
Code:
$ ./sfs -h

sfs - Substitute fixed strings.  This is a simple string
replacement utility. No regular expressions are involved, so no
escaping is required, except to shield strings from the shell if
the command-line options are used.

usage: sfs [options] -- [files]

options:
--all (or -a)
  Process entire line, otherwise only left-most bad string gets
  replaced.

--bad="string-to-be-replaced"
  Define the string that is not desired.

--good="replacement-string"
  Define the string that will replace the bad string.

  The -b and -g pairs may be repeated as necessary.

--file=pathname
  Specify a file containing pairs of lines, a bad instance
  followed by a good instance.

  The -b,-g pairs and the content of pathname are collected
  together and each data line is subjected to the search and
  replace operation for each pair.  The -b,-g feature is designed
  to allow short strings to be presented on the command line,
  whereas longer strings and many pairs may be placed in the
  pathname file.

--help (or -h)
  print this message and quit.

--version
  print the version and quit.
A sample usage script:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate sfs - substitute fixed strings.

sfs=sfs
sfs=./sfs

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) $sfs
set -o nounset

# Stage data files.

cat >data1 <<'EOF'
Non-alpha ?*[stuff] can be easily changed.
the quick brown fox jumps over the lazy dog
<?php @include("http://".$_SERVER['HTTP_HOST']."/linkingblogv.php"); ?>
EOF

cat >bad-good <<'EOF'
?*[stuff]
( cool again, "a[3] = 7 / 2" )
<?php @include("http://".$_SERVER['HTTP_HOST']."/linkingblogv.php"); ?>
GORILLA
EOF

echo
echo " Data file:"
cat data1

echo
echo " Results from command line:"
$sfs -b='*' -g=" star " data1

echo
echo " Results from strings file:"
$sfs -f=bad-good data1

exit 0
Producing:
Code:
$ ./s1

(Versions displayed with local utility "version")
FreeBSD 4.11-STABLE
sh - ( /bin/sh Mar 7 2007 )
sfs (local) 1.3

 Data file:
Non-alpha ?*[stuff] can be easily changed.
the quick brown fox jumps over the lazy dog
<?php @include("http://".$_SERVER['HTTP_HOST']."/linkingblogv.php"); ?>

 Results from command line:
Non-alpha ? star [stuff] can be easily changed.
the quick brown fox jumps over the lazy dog
<?php @include("http://".$_SERVER['HTTP_HOST']."/linkingblogv.php"); ?>
 ( Lines read: 3 )

 Results from strings file:
Non-alpha ( cool again, "a[3] = 7 / 2" ) can be easily changed.
the quick brown fox jumps over the lazy dog
GORILLA
 ( Lines read: 3 )
The code is almost 200 lines long (about 10% debug-instrumentation), so I'm not posting it here.

If anyone is interested, I can post the code as an attachment.

I don't visit every day, and the email notifications don't seem to work for me, so I'll respond when I can ... cheers, drl
Reply With Quote