JP Vossen on 16 Sep 2010 21:18:51 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Building a command from variables in BASH


Date: Thu, 16 Sep 2010 14:40:14 -0400
From: Michael Bevilacqua<michael@bevilacqua.us>
Subject: [PLUG] Building a command from variables in BASH


I know you already solved this, but I couldn't help commenting.


I have an ugly line that looks like this:

#cat $FILE1 | sed -e 's/            akrdcrc01<->cdp1crc02/     100003
akrdcrc01<->cdp1crc02/g' | sed -e 's/            ausrcrc01<->cdc1crc01/
100005 ausrcrc01<->cdc1crc01/g' | sed -e 's/
  cindcrc01<->cdc1crc02/     100002 cindcrc01<->cdc1crc02/g' | sed -e 's/
        daldcrc01<->cdp1crc01/     100004 daldcrc01<->cdp1crc01/g' | sed -e
's/            orarcrc01<->cdp1crc02/     100000 orarcrc01<->cdp1crc02/g' |
sed -e 's/            manrcrc01<->cdp1crc01/     100007
manrcrc01<->cdp1crc01/g' | sed -e 's/            sdgdcrc01<->cdc1crc02/
100001 sdgdcrc01<->cdc1crc02/g' | sed -e 's/
  syrrcrc01<->cdc1crc01/     100006 syrrcrc01<->cdc1crc01/g' | sed -e 's/
        cdc1crc01<->cdp1crc01/     100014 cdc1crc01<->cdp1crc01/g' | sed -e
's/            cdc1crc02<->cdp1crc02/     100015 cdc1crc02<->cdp1crc02/g'>
$FILE2

Yup, that's ugly.  :-)


So, to make it more portable, I did the following:

Portable?  Or maintainable/readable?


function makeVCID()
{
         for i in "100000 orarcrc01<->cdp1crc02" "100001
sdgdcrc01<->cdc1crc02" "100002 cindcrc01<->cdc1crc02" "100003
akrdcrc01<->cdp1crc02" "100004 daldcrc01<->cdp1crc01" "100005
ausrcrc01<->cdc1crc01" "100006 syrrcrc01<->cdc1crc01" "100007
manrcrc01<->cdp1crc01" "100014 cdc1crc01<->cdp1crc01" "100015
cdc1crc02<->cdp1crc02" ;

                 do echo -n '| sed -e 's/            `echo $i | cut -d" "
-f2`/     `echo $i | cut -d" " -f1` `echo $i | cut -d" " -f2`/g' ' ;

         done
}

I'm not sure that's much better. Also, you are spawning lots of sed sub-shells. Do you really need to? Can't you do something more like this:

sed -e 's/ orarcrc01<->cdp1crc02/ 100000 orarcrc01<->cdp1crc02/g' \
    -e 's/ sdgdcrc01<->cdc1crc02/ 100001 sdgdcrc01<->cdc1crc02/g' \
    -e 's/ cindcrc01<->cdc1crc02/ 100002 cindcrc01<->cdc1crc02/g' \
    -e 's/ akrdcrc01<->cdp1crc02/ 100003 akrdcrc01<->cdp1crc02/g' \
    -e 's/ daldcrc01<->cdp1crc01/ 100004 daldcrc01<->cdp1crc01/g' \
    -e 's/ ausrcrc01<->cdc1crc01/ 100005 ausrcrc01<->cdc1crc01/g' \
    -e 's/ syrrcrc01<->cdc1crc01/ 100006 syrrcrc01<->cdc1crc01/g' \
    -e 's/ manrcrc01<->cdp1crc01/ 100007 manrcrc01<->cdp1crc01/g' \
    -e 's/ cdc1crc01<->cdp1crc01/ 100014 cdc1crc01<->cdp1crc01/g' \
    -e 's/ cdc1crc02<->cdp1crc02/ 100015 cdc1crc02<->cdp1crc02/g' \
  $FILE1 > $FILE2

Gets rid of useless-use-of-cat (I can't believe no one else complained about that :) and I find it a heck of a lot more readable than the alternatives. It's also a LOT more efficient and faster, not that that usually matters with CPUs these days.

FWIW,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug