![]() |
|
|
|
|
|||||||
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
This is my approach to solve above problem.Experts please comment on this.
PHP Code:
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:
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. |
|
||||
|
Quote:
Code:
"s/pattern/replacement/global_flag" |
|
||||
|
Quote:
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
|
|
|||
|
Quote:
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. |
|
||||
|
Quote:
|
||||
| Google UNIX.COM |
| 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 |