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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
I am trying to change a line from a file
the line reads Code:
URL= 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 but if i do i get error and file is deleted Code:
sed: -e expression #1, char 48: unknown option to `s' 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=* |
| Sponsored Links | ||
|
|
|
|||
|
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 even ordered some books |
|
||||
|
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 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:
Code:
find -name test | cut -d. -f2
find -name test | sed -e 's/\.//g'
find -name test | awk -F'.' '{ print $2} '
Quote:
and practice it. |
|
|||
|
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 |
|
||||
|
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? |
|
||||
|
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 |
|
|||
|
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" Code:
grep include $filename | sed 's@"/>@@g' |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |