LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-07-2012, 07:34 PM   #1
david1985
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 07-08-2012, 09:13 AM   #2
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Rep: Reputation: 36
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.
 
Old 07-08-2012, 09:56 AM   #3
david1985
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-08-2012, 10:32 AM   #4
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Rep: Reputation: 36
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"
 
Old 07-08-2012, 10:45 AM   #5
david1985
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-08-2012, 11:53 AM   #6
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Rep: Reputation: 36
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).
 
Old 07-08-2012, 05:15 PM   #7
david1985
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-08-2012, 06:04 PM   #8
david1985
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-08-2012, 06:16 PM   #9
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Rep: Reputation: 36
Maybe [unct:]?
 
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration