nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

More trouble with sed :oops:

This is a discussion on More trouble with sed :oops: within the Shell scripting forums, part of the Development/Scripting category; I am trying to change a line from a file the line reads Code: URL= it may have web url ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 04-16-2006, 09:17 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default More trouble with sed :oops:

I am trying to change a line from a file
the line reads
Code:
URL=
it may have web url after it it may not .
Now i need to add a url to that line all one words the problem is it need to
start http://
Code:
URL=http://www.whatever.com
now it works fine if i dont add http:// to $url
but if i do i get error and file is deleted
Code:
sed: -e expression #1, char 48: unknown option to `s'
My code
Code:
 isp=$(cat file.txt | grep URL=*)


echo -n "Please enter new url"
echo "please include http:// in your address "
echo "EXAMPLE  http://noobtutorials.co.uk"
read url
URL="URL="$url""

sed -e "s/$isp/\"$URL\"/g"   file.txt > file1

mv file1  file.txt
echo -e "New url link is $url "
echo -e "If you do not see http:// before www. please try again"
cat server.ini  | grep URL=*
Thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-17-2006, 12:31 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

In string http:// double slash (//) confuses sed, as they are part of sed syntax. You need to replace // with \/\/ to disable its special meaning. For example:

Code:
URL="http://old.com"
olddomain="old.com"
newdomain="new.com"
echo $URL | sed "/http:\/\//s/$olddomain/$newdomain/g"
Modify your script and it should work now
Reply With Quote
  #3 (permalink)  
Old 04-17-2006, 06:54 AM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thanks for the reply but still can t get it working tried for hours now
i think the problem is i need it to work for both options
ie the file may say
URL=http://www.whatever.com
or it may just be blank
URL=

Also while i am here i need to find out where a program exists
so i found the find command works well but i need to turn it into a var
whats the best way to remove the . at the begining
Code:
[sparky@localhost ~]$ find -name test
./MyScripts/test
Any ways i am really loving linux scripting have more pleasure out of this than any thing else ive done on my pc
even ordered some books
Reply With Quote
  #4 (permalink)  
Old 04-17-2006, 12:31 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I think your approach is bit wrong here to solve problem. Anyways I got working script for you, replace your existing code with following script:

Code:
#!/bin/sh
# mod() is function
# must be at top of script
mod(){
NCF="$CF.$$"
[ ! -f $CF ] && touch $CF || :
V="$1"
grep -v -E "^$V=" $CF > $NCF
mv -f $NCF $CF
eval echo "$V=\"'\${$V}'\"">> $CF

}

# config file
CF="file.txt"
URL=""

echo -n "Please enter new url"
echo "please include http:// in your address "
echo "EXAMPLE  http://noobtutorials.co.uk"
read URL
# now change value for url
mod URL

echo -e "New url link is $URL "
echo -e "If you do not see http:// before www. please try again"
Variable CF stores file name, which you want to modify
Variable URL stores actual URL you want to setup

So script will ask for input i.e. new URL
Then function mod is called whice opens file stored in CF variable and replace or update URL it may have prefix http:// or without prefix http://


Quote:
whats the best way to remove the . at the begining,
Use any one of the following (cut is easy to use and small code):
Code:
find -name test  | cut -d. -f2  
find -name test  | sed -e 's/\.//g'
find -name test | awk -F'.' '{ print $2} '
Quote:
Any ways i am really loving linux scripting have more pleasure out of this than any thing else ive done on my pc even ordered some books
Same here Books are good, if you want to master programming, all you need to do is see good code (script and practice it.
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 04-17-2006, 04:48 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

Thanks nixcraft
I got a lot of reading to do before i understand that code in the mod function
still got two problems
you code works GREAT except it had single quotes either side of the new URl

any ideas
thanks
Reply With Quote
  #6 (permalink)  
Old 04-17-2006, 05:28 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

If you don't wanna ' (single quote) then remove it, i.e. replace following line in mod()
Code:
eval echo "$V=\"'\${$V}'\"">> $CF
with
Code:
eval echo "$V=\"\${$V}\"">> $CF
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #7 (permalink)  
Old 04-17-2006, 05:42 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

I must agree with sparky the code for mod function is high funda code. I just run on my system and I had no clue how it works

I think nixcraft has written it using all shell-scripting short forms that makes to read it difficult but may works very fast (small code works fast after all)

I guess sparky’s problem is almost solved and it would be great if nixCraft explains us what is cooking inside mod() function?
__________________
Friends - v-nessa - missyAdmin - LinuxChix
Reply With Quote
  #8 (permalink)  
Old 05-21-2006, 05:48 PM
Member
User
 
Join Date: Mar 2006
Posts: 35
Rep Power: 0
sparky
Default

even thou i don t understand it work great
But i still have one problems to iron out.
IT adds to URL=http:/www but it places at bottom of text doc how can i then move it back to line 17
Reply With Quote
  #9 (permalink)  
Old 05-22-2006, 02:13 AM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Ha ha

Old post just got some life again... entier stuff is overkilling, IMPO
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #10 (permalink)  
Old 07-13-2006, 10:21 PM
Junior Member
 
Join Date: Jul 2006
Posts: 1
Rep Power: 0
calum
Default

i had a similar problem with extracting lines from xml files. the solution is to change the delimiter that sed uses from the normal / to @ or any of following %,;:

in my case i was looking to remove the 3 characters from the end of a line in an xml file. The characters are: "/>
also note that i had to change to single quotes to ensure the characters are interpreted literally.

original failed implementation
Code:
grep include $filename | sed "s/"/>//g"
becomes:
Code:
grep include $filename | sed 's@"/>@@g'
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Open Source Trouble Ticket Application w/ Active Directory Integration Johnny Utah Linux software 3 03-05-2008 01:30 PM
oops proxy server on debian maroon Getting started tutorials 3 09-25-2007 06:37 PM


All times are GMT +5.5. The time now is 06:49 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36