LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   unable to replace "ö" to "p" in shell scripting with "sed" (https://www.linuxquestions.org/questions/programming-9/unable-to-replace-%F6-to-p-in-shell-scripting-with-sed-4175412683/)

meninmech 06-21-2012 10:54 AM

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

towheedm 06-21-2012 09:20 PM

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
or:
Code:

sed -e 's/ö/p/g' < ${f_name} > ${f_name}_new
Could you post your script? Please put your script within code tags.

meninmech 06-22-2012 01:18 AM

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

druuna 06-22-2012 01:54 AM

Quote:

# cat ${f_name} | sed -e 's/\${1}/${2}/g' > ${f_name}_new # this is not working
The single quotes used in the sed command will prevent the shell from expanding the variables used. Use double quotes instead.

Give this a try:
Code:

sed "s/${1}/${2}/g"
EDIT: If single quotes do need to be used you can also do this (not as clean in my opinion):
Code:

sed 's/'${1}'/'${2}'/g'

meninmech 06-22-2012 06:36 AM

Hi druuna ,

Thank you very much ... its works perfectly now....its a quite a learning

:) :)


Thx
Meninmech

David the H. 06-22-2012 02:58 PM

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.