The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
parameters jaay Shell Programming and Scripting 4 04-14-2008 01:10 AM
Max NO of parameters Shivdatta Shell Programming and Scripting 1 07-24-2006 05:11 AM
parameters aekaramg20 Shell Programming and Scripting 6 07-10-2006 10:15 PM
Need Parameters Help. james2006 Shell Programming and Scripting 3 06-08-2006 07:46 AM
tar parameters kmar UNIX for Advanced & Expert Users 4 10-23-2001 12:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2007
Registered User
 

Join Date: Mar 2007
Posts: 17
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
sed and parameters

Hello

I'm trying to write shell script "replace.sh" but I have following problem :
My script take three parameters like this:
./repalce.sh <pairs > <input_file > < output_file >
Where
<pairs> = p1/w1,p2/w2 , .. ,pn/wn .
My script change all ocurrences p1 to w1 , p2 to w2 ... pn to wn
in <input_file > and write all the changes to output_file .
I'm trying to write the script with 'sed' but I have problem with the
strange argument <pairs > - there is no space between p1/w1,p2/w2,.. ,pn/wn
but only ',' .

I was trying to split <pairs> by changing ',' sign on space , but it don't work.
Code:
par=eval echo $1 | tr  ','  ' '       
for p in $par ; do                    
   sed "s/$p/g/" $2 
done
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-24-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 221
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
This is my approach to solve above problem.Experts please comment on this.

PHP Code:
#!/bin/ksh
OLD_IFS=$IFS
PAIRS
=$1
IN_FILE
=$2
LINES
=`cat $2 | wc -l`
OUT_FILE=$3
let count
=0
IFS
=,
rm $OUT_FILE
touch $OUT_FILE
for P in $PAIRS ; do
  if [[ 
count -eq 0 ]] ; then
    IN_FILE
=$IN_FILE
  
else
   
IN_FILE=$OUT_FILE
  fi
  
echo $P
  OLD
=${P%/*}
  NEW=${P##*/
}
  echo 
$OLD $NEW
  sed 
"s/$OLD/$NEW/g" $IN_FILE >> $OUT_FILE
  
((count=count+1))
done
tail 
-n $LINES $OUT_FILE  > /tmp/tmp.file
mv 
/tmp/tmp.file $OUT_FILE

Results:

cat input
p1 w1
p2 p1 w1 w3
p3 p4
p33 p2 p24 p1 p4
p4 p2
p2
p1

Now upon executing the script,
PHP Code:
./replace.ksh p1/w1,p2/w2,p3/w3,p4/w4 input out 
cat out
w1 w1
w2 w1 w1 w3
w3 w4
w33 w2 w24 w1 w4
w4 w2
w2
w1

Please let me know if this works out.

Thanks
Nagarajan Ganesan.
Reply With Quote
  #3 (permalink)  
Old 03-25-2007
Am not alone now!!!!
 

Join Date: Feb 2007
Location: Cochin, India
Posts: 242
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Try this out.......

Code:
for each in $(echo $1 | tr  ','  ' ' )
do
 sed 's/'$each'/g'  $2 >temp
 mv temp $2
done
cp $2 $3

Last edited by dennis.jacob; 03-26-2007 at 01:09 AM.
Reply With Quote
  #4 (permalink)  
Old 03-26-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,628
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Since your input parameter $1 is of the form pattern/replacement, you can use it to your advantage.

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

echo $1

par=$(echo $1 | tr  ','  ' ')
echo $par
finalPattern=""
for pattern in $par
do
    finalPattern=${finalPattern}"s/$pattern/g;"
done

sed -e "${finalPattern}" $2
[/tmp]$ cat in
p1 w1
p2 p1 w1 w3
p3 p4
p33 p2 p24 p1 p4
p4 p2
p2
p1
[/tmp]$ ./try.ksh p1/w1,p2/w2,p3/w3,p4/w4 in
p1/w1,p2/w2,p3/w3,p4/w4
p1/w1 p2/w2 p3/w3 p4/w4
w1 w1
w2 w1 w1 w3
w3 w4
w33 w2 w24 w1 w4
w4 w2
w2
w1
[/tmp]$
Reply With Quote
  #5 (permalink)  
Old 03-26-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,628
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by jacoden
Try this out.......

Code:
for each in $(echo $1 | tr  ','  ' ' )
do
 sed 's/'$each'/g'  $2 >temp
 mv temp $2
done
cp $2 $3
Your regex expression is not valid. For regex, sed requires an expression of the format
Code:
"s/pattern/replacement/global_flag"
You have "s/pattern/global_flag". Also the single quote will disable parameter expansion.
Reply With Quote
  #6 (permalink)  
Old 03-26-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,628
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by ennstate
This is my approach to solve above problem.Experts please comment on this.

PHP Code:
#!/bin/ksh
OLD_IFS=$IFS
PAIRS
=$1
IN_FILE
=$2
LINES
=`cat $2 | wc -l
OUT_FILE=$3
let count
=0
IFS
=,
rm $OUT_FILE               
touch $OUT_FILE     
for P in $PAIRS ; do
  if [[ 
count -eq 0 ]] ; then
    IN_FILE
=$IN_FILE
  
else
   
IN_FILE=$OUT_FILE
  fi
  
echo $P
  OLD
=${P%/*}
  NEW=${P##*/
}
  echo 
$OLD $NEW
  sed 
"s/$OLD/$NEW/g" $IN_FILE >> $OUT_FILE
  
((count=count+1))
done
tail 
-n $LINES $OUT_FILE  > /tmp/tmp.file
mv 
/tmp/tmp.file $OUT_FILE

Code:
#!/bin/ksh
OLD_IFS=$IFS
PAIRS=$1
IN_FILE=$2
LINES=`cat $2 | wc -l`   # LINES=$(wc -l < $2)
OUT_FILE=$3
let count=0
IFS=,
rm $OUT_FILE
touch $OUT_FILE          # The rm $OUT_FILE and touch $OUT_FILE can be replaced with :>$OUTFILE
for P in $PAIRS ; do
  if [[ count -eq 0 ]] ; then
    IN_FILE=$IN_FILE
  else
   IN_FILE=$OUT_FILE
  fi
  echo $P
  OLD=${P%/*}
  NEW=${P##*/}
  echo $OLD - $NEW
  sed "s/$OLD/$NEW/g" $IN_FILE >> $OUT_FILE
  ((count=count+1))
done
tail -n $LINES $OUT_FILE  > /tmp/tmp.file
mv /tmp/tmp.file $OUT_FILE
Reply With Quote
  #7 (permalink)  
Old 03-26-2007
Am not alone now!!!!
 

Join Date: Feb 2007
Location: Cochin, India
Posts: 242
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by vino
Your expression is not valid. sed requires an expression of the format
Code:
"s/pattern/replacement/global_flag"
You have "s/pattern/global_flag". Also the single quote will disable parameter expansion.

No Vino...You are wrong.As you said in your previous post that we can take advantage of the pattern of "p1/w1" here....
So I just used it here... It works well for me..

Here, Every $each stands for "p1/w1" , "p2/w2" etc....
sed 's/'$each'/g' $2
So in effect, it will be
sed 's/p1/w1/g $2

Last edited by dennis.jacob; 03-26-2007 at 01:23 AM.
Reply With Quote
  #8 (permalink)  
Old 03-26-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,628
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by jacoden
No Vino...You are wrong.As you said in your previous post that we can take advantage of the pattern of "p1/w1" here....
So I just used it here... It works well for me..

Here, Every $each stands for "p1/w1" , "p2/w2" etc....
sed 's/'$each'/g' $2
Right. That slipped through.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection ^m autosys awk trim bash eval bash exec bash for loop boot: cannot open kernel/sparcv9/unix close_wait command copy/move folder in unix curses.h cut command in unix dead.letter find grep find null character in a unix file grep multiple lines grep or grep recursive grep unique inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp batch sftp script snoop unix stale nfs file handle syn_sent tar exclude unix unix .profile unix com unix forum unix forums unix interview questions unix mtime unix simulator unix.com vi tab size vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 12:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101