unable to replace "ö" to "p" in shell scripting with "sed"
Dear experts ,
I have a file "${f_name}" with sample values like 000613380289.Gönderi 000613380290.Gönderi I need to replace the character "ö" with "p". cat ${f_name} | sed -e 's/ö/p/g' > ${f_name}_new if i execute the above command in command prompt , its working fine.... but if i use the same command inside a shell script and try to execute , its not replacing. please let me know if i need to add further details here... Thx meninmech |
I tried it and it works from within a script. Firstly, there is no need to pipe cat to sed. The first parameter after sed's substitute command is the input file that sed should read. The output can then be re-directed to another file:
Code:
sed -e 's/ö/p/g' ${f_name} > ${f_name}_new Code:
sed -e 's/ö/p/g' < ${f_name} > ${f_name}_new |
1 Attachment(s)
Hi towheedm,
Thx a lot for ur suggestions. Actually,i missed to add #!/bin/ksh at the beginning of the script.. Now the sed is working like a charm. but the script works only if i hard core the values...i can't parse the $search and $replace as parameters. Please find the script in the attachment.. Also have pasted it here... #!/bin/ksh ############################################# # Function for searching # ############################################# Special_char_Search () { echo "Initialized for $1" echo "" grep -l "$1" * > A_list.txt export t_file=`cat A_list.txt | wc -l` echo "The no of files to be processed for $1 :" ${t_file} echo "" if [ ${t_file} -gt 0 ] then cat A_list.txt | while read line do export f_name=$line echo "Processing File Name :" ${f_name} # cat ${f_name} | sed -e 's/\${1}/${2}/g' > ${f_name}_new # this is not working cat ${f_name} | sed -e 's/ö/o/g' -e 's/ä/a/g' > ${f_name}_new echo "string $1 replaced as $2 in ${f_name}" echo "" cat A_list.txt | grep -v $f_name > A_list.txt done else echo "No Files to be processed for $1. " fi } # Main module starts here ############################################# # The hunt for Junk character # ############################################# echo "Initiating the program to search special characters" echo "" Special_char_Search ö o Special_char_Search ä a |
Quote:
Give this a try: Code:
sed "s/${1}/${2}/g" Code:
sed 's/'${1}'/'${2}'/g' |
Hi druuna ,
Thank you very much ... its works perfectly now....its a quite a learning :) :) Thx Meninmech |
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
One small addition to the above. You really should never leave a variable unprotected. So single quote the parts that need to be literal, and double-quote the parts that need expansion. (And lose the brackets around the variables. They are unnecessary in most cases, and do nothing but add clutter to the code.) Code:
sed 's/'"$1"'/'"$2"'/g' In detail: you should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later. http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes (The site is bash-centric, but the above applies to all bourne-style shells.) |
All times are GMT -5. The time now is 12:20 AM. |