Find the answer to your Linux question:
Results 1 to 3 of 3
#!/usr/bin/perl #===========whitelist start======================= $whitelist = $whitelist . " test\@yahoo.com test34\@gmail.com "; #===========whitelist end======================= #===========blacklist start===================== $blacklist = $blacklist . " test\@gmail.com test987\@yahoo.com "; #===================blacklist end=============== I have a file containing ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    get variable on certain portion of a file


    #!/usr/bin/perl

    #===========whitelist start=======================
    $whitelist = $whitelist . "
    test\@yahoo.com
    test34\@gmail.com
    ";
    #===========whitelist end=======================


    #===========blacklist start=====================
    $blacklist = $blacklist . "
    test\@gmail.com
    test987\@yahoo.com
    ";
    #===================blacklist end===============
    I have a file containing above. I need to create 2 variables. 1 variable containing the text inside whitelist start and whitelist end and 1 variable containing the text inside blacklist star and blacklist end. What will happen is variable 1 will have test\@yahoo.com and test34\@gmail.com while variable 2 is test\@gmail.com and test987\@yahoo.com

    Is there a way I can do this by making a script.

    TIA

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,286
    Hi.

    I find a non-standard utility cgrep useful for tasks like this. Here is a sample script that extracts the whitelist section of the data:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate extraction of pattern-bounded text, cgrep.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cgrep
    set -o nounset
    echo
    
    FILE=${1-data1}
    
    echo " Data file $FILE:"
    cat $FILE
    
    echo
    echo " Results:"
    v1=$(cgrep -D -I2 -w 'whitelist start' +I2 +w 'whitelist end' '$whitelist' $FILE)
    echo " variable v1 is:"
    echo "$v1"
    
    exit 0
    producing:
    Code:
    % ./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 
    GNU bash 3.2.39
    cgrep - (local: ~/executable/cgrep May 29 10:59 )
    
     Data file data1:
    #!/usr/bin/perl
    
    #===========whitelist start=======================
    $whitelist = $whitelist . "
    test\@yahoo.com
    test34\@gmail.com
    ";
    #===========whitelist end=======================
    
    
    #===========blacklist start=====================
    $blacklist = $blacklist . "
    test\@gmail.com
    test987\@yahoo.com
    ";
    #===================blacklist end=============== 
    
     Results:
     variable v1 is:
    $whitelist = $whitelist . "
    test\@yahoo.com
    test34\@gmail.com
    ";
    The -I2 -w options specify that the start of the context window is defined by a following string, but that the line itself should not be included, similarly with the +I2 +w options.

    The cgrep source is at cgrep home page I have compiled it under 32-bit systems, and recently under a 64-bit system (Debian), both with no trouble.

    Best wishes ... cheers, drl
    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 )

  3. #3
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    thanks for the reply. It is a big help!!!

Posting Permissions

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