[Solved] Command Substitution and Variable Expansion within a Case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Command Substitution and Variable Expansion within a Case
# 1  
Old 01-11-2011
[Solved] Command Substitution and Variable Expansion within a Case

Hello All,

I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system).

I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other criteria.

For this example, I want to extract...
Code:
  Reception_1

from the following string
Code:
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>

that is buried in the following chunk of text (that is pulled from an event log file in another process)
Code:
  14:10:03: CONNECT:      A5BF OcPhone: Reception_1,     OcLine: Reception_1,         <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcHuntGroup: Operator_Group, <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcStaticCall: System, <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: IDLE:0,0      25BF OcSIPGatewayNew: T1G1,    OcStreamer: System,
  14:10:03: CONNECT:      25BF OcSIPGatewayNew: T1G1,    OcTrunkGroup: T1,            <audio=10.0.0.2:11728 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcStaticCall: System,        <audio=10.0.0.2:11728 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: ACTIVE:       25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcPhone: Reception_1,     OcLine: Reception_1,         <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcHuntGroup: Operator_Group, <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcStaticCall: System, <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>



I am trying to do this with the following snippet of bash script from within a case statement:
Code:
#!/bin/bash
#GNU bash, version 2.05b.0(1)-release
#

    IFS=$'\t\n'                                     # 
    CDRF12="OcHuntGroup:~Operator_Group~~~"         # CDR Destination, from another process
    TimeAnsLoc="11/15/2010~14:10:03"                # CDR Date from another process
    EVTFILE="/var/log/eventfile.txt.gz"             # Event file from another process
    EVTSRCH="[2,A]5BF"                              # Event OID set, from another process
    DSTENDT=""                                      # Destination EndPoint Type
    DSTENDN=""                                      # Destination EndPoint Name
    DSTAGNT=""                                      # Destination Agent from EventLog (Huntgroups, Queues)
    case "${CDRF12:0:6}" in
            "OcHunt") DSTENDT="HG"                 # To Huntgroup
                      DSTENDN="${CDRF12:13:30}"    # OcHuntGroup: ~~~~~~~~~~~~~~~~~ ${CDRF11:13:30}
                      EVTSRCH="$(echo "\"${TimeAnsLoc:11:19}: CONNECT:.*""$EVTSRCH"" OcHuntGroup\"")"
                      DSTAGNT="$(zgrep $EVTSRCH $EVTFILE | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3 )"
                      ;;
                  * ) DSTENDT="UK"                  # Unknown
                      DSTENDN="UNKNOWN"             # Also Unknown
                      ;;
    esac
    #Testing
    echo "CDRF12  = $CDRF12"
    echo "TimeAnsLoc = $TimeAnsLoc"
    echo "EVTSRCH = $EVTSRCH"
    echo "EVTFILE = $EVTFILE"
    echo "SEARCH Expansion = zgrep $EVTSRCH $EVTFILE | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3"
    echo "DSTAGNT = $DSTAGNT"


When I run this, I get the following output:
Code:
> CDRF12  = OcHuntGroup:~Phil~Crisp~Agency
> TimeAnsLoc = 11/15/2010~14:10:03
> EVTSRCH = "14:10:03: CONNECT:.*[2,A]5BF OcHuntGroup"
> EVTFILE = /var/log/eventfile.txt.gz
> SEARCH Expansion = zgrep "14:10:03: CONNECT:.*[2,A]5BF OcHuntGroup" /var/log/eventfile.txt.gz | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3
> DSTAGNT =

As you can see, DSTAGNT is null, and as best I can tell the zgrep command in the case is not being evaluated. Running the expanded zgrep search from the command line works fine.

Any assistance will be appreciated in determining why this is so.

Jaimie Smilie


---------- Post updated at 05:40 PM ---------- Previous update was at 02:28 PM ----------

Solved.

Found/Remembered use of "eval" in a case structure....
# 2  
Old 01-12-2011
Thanks for keeping us informed

All the best
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful

Hi, at academic level I am familiar with how variable substitution/expansion feature works. From your live experience, can you please tell what are real-time scenarios where ${variable:=} ${variable%pattern} ${variable:=word} sort of features can be used? How can we connect that dot that... (6 Replies)
Discussion started by: ab_2010
6 Replies

2. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

3. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

4. Shell Programming and Scripting

[Solved] cp command with dollar variable in ksh

hi, I have been trying to acheive the following task for a while now, but failed.. Need help, experts please help! This is what I am trying to do: - I am writing to a flat file the name of the source to be copied and the destination path as to where it is to be copied to. Sample flat file:... (7 Replies)
Discussion started by: abdulhusein
7 Replies

5. UNIX for Dummies Questions & Answers

Variable inside command substitution

Hello people. Part of my script: SUBSETID=`echo $PMFILE |sed 's/pmresult_//' | sed 's/_*//'` MAPFILE=`find /huawei/cell /huawei/nodeb /huawei/rnc -name 'mapping_$SUBSETID.txt' |grep -v backup` Unfortunatelly variable $SUBSETID in the MAPFILE declaration will not return the... (3 Replies)
Discussion started by: drbiloukos
3 Replies

6. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

7. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

8. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

9. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

10. Shell Programming and Scripting

help with variable substitution in case statement

I have a script where I take input from user and see if it falls in list of valid entry. --------------- #!/bin/ksh VALID_ENTRIES="4|5|6" echo "enter your choice" read reply IFS="|" echo "valid entries are $VALID_ENTRIES" case "$REPLY" in Q|q ) IFS="$IFS_SAVE";return ;;... (5 Replies)
Discussion started by: bhav_shah82
5 Replies
Login or Register to Ask a Question