LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-21-2012, 10:54 AM   #1
meninmech
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
Exclamation 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
 
Old 06-21-2012, 09:20 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
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.
 
Old 06-22-2012, 01:18 AM   #3
meninmech
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
Attached Files
File Type: txt string_replace.txt (1.1 KB, 1 views)
 
Old 06-22-2012, 01:54 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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'

Last edited by druuna; 06-22-2012 at 01:59 AM.
 
1 members found this post helpful.
Old 06-22-2012, 06:36 AM   #5
meninmech
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi druuna ,

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




Thx
Meninmech
 
Old 06-22-2012, 02:58 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
Sed scripting "how to replace value for only last part of a pattern " alok.rhct Linux - General 3 04-28-2010 07:10 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM


All times are GMT -5. The time now is 09:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration