Passing a value to a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a value to a condition
# 1  
Old 10-20-2011
Passing a value to a condition

All

Hopefully the title is not too misleading but I'm hitting a stumbling block. I have a file that looks like this.

Code:
<create_context date="2010-12-31" id="IMAP-20101231-20111020201249" name="IMAP-20101231" timestamp="2010-12-31">
      <instrument>
         <file>/tmp/instruments.txt</file>
      </instrument>
      <mtf>
         <cube id="045_FULLMC">
            <manifest>11.csr</manifest>


I loop through this file reach the cube id line where I strip out the 045 integer value and the idea is to then create a new line as follows

Code:
<directory>/tmp/045</directory>



I also process the cube id line. So in the end, my file should look like this.

Code:
<create_context date="2010-12-31" id="IMAP-20101231-20111020201249" name="IMAP-20101231" timestamp="2010-12-31">
      <instrument>
         <file>/tmp/instruments.txt</file>
      </instrument>
      <portfolio>
         <directory>/tmp/045</directory>
      </portfolio>
      <mtf>
          <cube id="FULLMC">
            <manifest>11.csr</manifest>



The code works fine for 99% of what I need to do. Where I am struggling is in passing the correct cube id. So instead of /tmp/045 I see my original declaration of $cubeid which is 000. Because it's doing a while loop, $cubeid will always be 000 for the first 10 or so lines before it reaches the cube id line but by then it seems too late.

Code:
my $cubeid='000';
my $portexists=0;

push @OUT, "<contexts>\n";

while(<>){
        $cubeid=$1 if s/cube id="(\d+)_(\w+)">/cube id="$2">/;
        $portexists = 1 if m/<portfolio>/;
        if (m#<mtf># and $portexists == 0 ){
        push @OUT, "\t<portfolio>\n";
        push @OUT, "\t\t<directory>$dynPath/$portFile/$cubeid</directory>\n";
        push @OUT, "\t</portfolio>\n";
        }
}
push @OUT, "</contexts>\n";

open(WF,">${cubeid}_create_context.xml");
print WF @OUT;

It seems rather trivial but I have been at this for a while but no joy
. any help appreciated as always.

Last edited by vgersh99; 10-20-2011 at 06:41 PM.. Reason: code tags, please!
# 2  
Old 10-20-2011
You'll need to parse the file twice to get the right cube id when you encounter "<mtf>" - otherwise you're already late...
Something along these lines in awk - not as succinct as perl, but.....
nawk -f king.awk myFile
king.awk:
Code:
BEGIN{ ARGV[ARGC++] = ARGV[1] }
FNR==NR {if(/cube id=/ && match($0,"[0-9]+_")) {cubeA[++c]=substr($0, RSTART, RLENGTH-1);rs[c]=RSTART;rl[c]=RLENGTH}; next}
/mtf/ {
   printf("\t<portfolio>\n\t\t<directory>/tmp/%s<directory>\n\t</portfolio>\n", cubeA[++c2])
}
/cube id=/ {$0=substr($0,1,rs[c2]-1) substr($0, rs[c2]+rl[c2])}
1


Last edited by vgersh99; 10-20-2011 at 07:29 PM..
# 3  
Old 10-20-2011
Thank vgersh99.. can't say I understand what the awk script does but I'll give it a try. Thanks for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with if condition

o/p of my command is given below My requirement is if Pnumber is 0 then stabilization.Build.2013 else stabilization.PBuild.2013.3 (11 Replies)
Discussion started by: nikhil jain
11 Replies

2. Shell Programming and Scripting

Using if condition

Hello, I want to use if condition in an expression as below: $ORACLE_HOME is a variable something like below /oraprod04_01/app/oracle/product/10204 Now here it is product/10204 I want to check if $ORACLE_HOME has something which has a string like /product/10* then one statement should... (4 Replies)
Discussion started by: Vishal_dba
4 Replies

3. Shell Programming and Scripting

If condition

Please help me new to shell getting error while running below in shell script #!/bin/bash set -x cd /abc/def/ghi pwd xyz1=ghi if then FAILURE_TEMP="The ghi directory has not been properly defined for this server." echo ${FAILURE_TEMP} | /bin/mailx -s "apps copy failed"... (4 Replies)
Discussion started by: buzzme
4 Replies

4. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. Shell Programming and Scripting

help with if condition

I do have a situation where , i need to zip the log files in the directory when the file exceeds more than 10MB. cd $ORACLE_HOME/network/log find . -type f -name "listener_*.log" > listeners Now i have all my *.log files listed in the listeners file So now i need to find the size of each... (5 Replies)
Discussion started by: naveen529
5 Replies

9. Shell Programming and Scripting

if condition

Hi friends, :) In a shell script i found the following if condition. echo -n "Which version of $1 do you want to restore ('0' to quit)? : " read desired if ${desired:=1} -ge $index ] ; then echo "$0: Restore canceled by user: index value too big." >&2 exit 1 fi Can... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

10. Shell Programming and Scripting

if condition ...

i have following if condition if above statement is case sensitive.....what is syntax if i have to make above comparision case insensetive (4 Replies)
Discussion started by: mahabunta
4 Replies
Login or Register to Ask a Question