Find the answer to your Linux question:
Results 1 to 1 of 1
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    String substitution (not regular expressions)


    I was answering a question in thread and thought I would post a general solution here because the question was not SuSE-specific. See the thread for details and background.

    Here is a driver script for the perl code in attached file p1.txt:
    #!/usr/bin/env bash
    # @(#) s1	Demonstrate non-regex string replacement, perl.
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C perl ./p1
    [ -x $HOME/bin/divepm ] && ( pe " perl module versions:";divepm -q -i ./p1 )
    pl " Input data file data1:"
    cat data1
    pl " Results:"
    ./p1 abc def data1
    pl " Input data file data2:"
    cat data2
    pl " Replace special characters (usually need shell quoting):"
    ./p1 "!@#$%^&*()_+=-" "|ah|" data2
    pl " Input file data3:"
    cat data3
    pl " Replace, like sed default, no global:"
    ./p1 abc def data3
    pl " Replace, global replace:"
    ./p1 -g abc def data3
    exit 0
    which produces:
    % ./s1
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    GNU bash 3.2.39
    perl 5.10.0
    ./p1 - ( local: RepRev 1.9, ./p1, 2011-08-13 )
     perl module versions:
     1.06	warnings
     1.04	strict
     1.11	feature
     Input data file data1:
     Input data file data2:
     Replace special characters (usually need shell quoting):
     Input file data3:
     Replace, like sed default, no global:
     Replace, global replace:
    You'll need to download the file p1.txt, save as p1, add execute permission, and then copy/paste the data files and shell script to run the demo on your machine.

    Best wishes ... cheers, drl

    ( edit: minor cosmetic changes; remove code for domestic version )
    Attached Files Attached Files
    Last edited by drl; 08-13-2011 at 08:29 PM.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts