Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-07-2012, 07:34 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Rep:
|
shell script help
I am creating a new script, sub2, taking three parameters, that treats the string to be replaced as plain text instead of as a regular expression.
The script works for everything except spaces.
#!/bin/csh
set firstString="$1"
set secondString="$2"
set file="$3"
#adds on escape characters to be used later in sed
set arg=`echo "$firstString" | sed 's:[]\[\^\$\.\*\/]:\\&:g'`
sed "s/$arg/$secondString/g" "$file" > "$file.updated"
mv "$file.updated" "$file"
from my terminal here are a few examples:
sub2 o a test.txt
test.txt will change from
"hello world" to "hella warld"
sub2 . ! test.txt
"hello..." to "hello!!!"
my problem is I do not understand much about shell scripting or sed in general.
when i type :
sub " " x test.txt
I expect all the spaces to be replaced with "x".
But I get an error:
sed: -e expression #1, char 0: no previous regular expression
Any help would be greatly appreciated.
|
|
|
07-08-2012, 09:13 AM
|
#2
|
Member
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213
Rep:
|
Hi there,
Maybe you should use sed with a -e option:
Code:
sed -e "s/$arg/$secondString/g" "$file" > "$file.updated"
Hope it helps, tell if not.
EDIT:
1.- can you post the value of $arg?
2.- Shouldn't you escape secondString too? At least for slashes and variables...
Last edited by emi_ramo; 07-08-2012 at 09:15 AM.
|
|
|
07-08-2012, 09:56 AM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep:
|
sed -e "s/$arg/$secondString/g" "$file" > "$file.updated"
with
echo "$firstString"
echo "$secondString"
echo "$file"
echo "$arg"
this was the output:
sub2 " " a test1.txt (command I ran from the terminal) spaces indicate the values firstString and secondString
a
test1.txt
sed: -e expression #1, char 0: no previous regular expression
So still not there yet. If I eventually figure it out ill post on here what I was missing.
|
|
|
07-08-2012, 10:32 AM
|
#4
|
Member
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213
Rep:
|
Hi again,
What about using bash instead of csh?
Code:
#!/bin/bash
firstString="$1"
secondString="$2"
file="$3"
#adds on escape characters to be used later in sed
arg=$( echo "$firstString" | sed 's:[]\[\^\$\.\*\/]:\\&:g' )
sed -e "s/$arg/$secondString/g" "$file" > "$file.updated"
mv "$file.updated" "$file"
|
|
|
07-08-2012, 10:45 AM
|
#5
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep:
|
I am using ssh to connect to the linux box. So I don't think I have that option. It should work on either but I'm not sure. Why I am posting on the newbie forum.
|
|
|
07-08-2012, 11:53 AM
|
#6
|
Member
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213
Rep:
|
Hi again,
Sorry, I don't know nearly anything about csh. I know it's doing some substitutions differently from what bash does (it strips initial/final spaces, for example). To use bash in your script instead of csh you only need to change csh to bash in the hash bang (#!/bin/bash, script's first line). Then, you can stop using 'set' and you can use $(command) instead of `command`. The example I posted before works as expected if bash is installed in the system (nearly all modern linux have it installed).
|
|
|
07-08-2012, 05:15 PM
|
#7
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep:
|
I tried what you suggested and it isn't working on cases that previously were. Would you please post what you got working so I know that I changed everything properly.
|
|
|
07-08-2012, 06:04 PM
|
#8
|
LQ Newbie
Registered: Jul 2012
Posts: 5
Original Poster
Rep:
|
I switched it over to sh correctly. Now the expression arg="`echo "$firstString" | sed 's:[]\[\^\$\.\*\/]:\\&:g'`" isnt returning the proper variable.
What is a good expression to change special characters to special characters with escape characters.
For example * to \* etc
|
|
|
07-08-2012, 06:16 PM
|
#9
|
Member
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213
Rep:
|
Maybe [ unct:]?
|
|
|
All times are GMT -5. The time now is 06:24 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|