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 ...
- 06-10-2009 #1Linux Newbie
- Join Date
- Mar 2006
- Posts
- 101
get variable on certain portion of a file
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#!/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===============
Is there a way I can do this by making a script.
TIA
- 06-10-2009 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
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:
producing: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
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.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 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, drlWelcome - 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 )
- 06-11-2009 #3Linux Newbie
- Join Date
- Mar 2006
- Posts
- 101
thanks for the reply. It is a big help!!!


Reply With Quote