Perl Substition with multiple conditions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl Substition with multiple conditions
# 1  
Old 09-25-2008
PHP Perl Substition with multiple conditions

I have a text file that contains lines similar to the following:

-----------------------------------------------------------
fall for setup

CSHRC0 'gnd';
CSHR0 'gnd';

rise for setup

rise for hold

CSHRC0 'gnd';
CSHR0 'gnd';

fall for hold
------------------------------------------------------------

I am trying to replace the gnd in between fall for setup and rise for setup with 0.75*vdd.

I tried the following:
Code:
perl -e 's/gnd/0\.75\*vdd/ if m/CSHRC0/ && /fall\ for\ setup/ .. /rise\ for\ setup/' -pi file
perl -e 's/gnd/0\.75\*vdd/ if m/CSHR0/ && /fall\ for\ setup/ .. /rise\ for\ setup/' -pi file

But these don't seem to work at all. I believe the problem is with how I am using the && operator to combine both conditions, since if I use any of those two conditions separately they would work. How can I apply multiple if conditions to the same substitution?
# 2  
Old 09-25-2008
Try parentheses.

Code:
perl -e 's/gnd/0\.75\*vdd/ if (m/CSHRC0/ && /fall\ for\ setup/ .. /rise\ for\ setup/)'

# 3  
Old 09-25-2008
I tried using () and it still didn't change anything. Is this platform dependent? I think I am using UNIX with c shell right now.
# 4  
Old 09-25-2008
Sorry for the false diagnostic; it was a shot from the hip and I should have tested before posting. The real problem is in how /x/ .. /y/ is implemented -- it will fail to see the starting condition if the /CSHRC0/ condition in front of it fails! So just reverse those conditions.

Code:
perl -pe 's/gnd/0.75*vdd/ if /fall for setup/ .. /rise for setup/ && /CSHRC?0/'

I took out some redundant backslashes, too.

Are you replacing both occurrences of gnd? Then the single condition /CSHRC?0/ should cover both of those. Take out the question mark and adapt accordingly if that was not what you meant.

Last edited by era; 09-25-2008 at 03:52 PM.. Reason: Question mark in regex to cover both gnd occurrences
# 5  
Old 09-25-2008
Thank you very much for the quick response. I tried reversing it, but somehow all the instances of gnd in the file got replaced by 0.75*vdd. So I tried Parentheses for the range condition and then it worked out fine.

Code:
perl -e 's/gnd/0\.75\*vdd/ if (/fall for setup/ .. /rise for setup/) && m/CSHRC?0/' -pi file

Thanks again for the help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple If conditions

I am analyzing one of the scripts written by another person.script is having multiple if conditions and everything are nested.The code is not formatted properly.Is there any way to identify in Unix to identify begin and end of a particular if block? (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

2. Shell Programming and Scripting

Multiple conditions in IF

Fellas, Am new to unix os/ and here the situation , I am trying to write multiple condition statement inside if but it throws me a error here is my piece of code , if ] && ] && ] then commands fi error : line 15 : ` can someone please advise me how to fix it Please use... (7 Replies)
Discussion started by: xeccc5z
7 Replies

3. Shell Programming and Scripting

Using sed and if multiple conditions

Hi I've been trying to get this to work but so far no luck. #!/usr/bin/ksh unset EXP APP=$1 EXP=`sed -n "/${APP}/p" tIclrpt.out |awk '{print $7}'|sed '/^$/d'` EXT=`sed -n "/${i}/p" ${SESLOG} |awk '{print $4}'|grep "${i}"` EXEM=/path/to/fail ACTI=/path/to/success if || then... (10 Replies)
Discussion started by: techy1
10 Replies

4. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

5. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

6. UNIX for Dummies Questions & Answers

Nested if with multiple conditions

Deal Experts I am working on a script to find a date which is 7 days older and follwoing is my approach #!/bin/sh Yr=`date +"%Y"` Mn=`date +"%m"` Md=28 Da=`date +"%d"` echo $Yr echo $Mn echo $Da var1=$Yr$Mn$Da echo "before" $var1 if expr $Da > 7 then Da=`expr $Da - 7`... (3 Replies)
Discussion started by: sweetnsourabh
3 Replies

7. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

8. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

9. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

10. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies
Login or Register to Ask a Question