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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-15-2006, 01:34 PM
|
#1
|
|
Member
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Rep:
|
modifying a file in shell (via sed ?)
Hello
here is my problem : i want to change one line of a file with another, but can't find how to do it :[
from what i've seen on the net, i should use sed, but i have a big problem with it because of some slashes that mess up the command line....
>
Quote:
#!/bin/bash
images=$(find /home/babou/classes/ -type f \( -name "*.svg" -o -name "*.png" \) -printf "%p;")
# now i have in image all the pngs and svgs in the directory
sed -e 's/beginning=.*/'$images'/' ~/bibi/file
# that should change the line in "file" with "beginning=" in it with $images
|
i'm not used to shell so it took me some time to see where is the problem, and in fact it is because there are "/" in $images so that sed has too much "arguments" (as it uses "/" to separate the "arguments")
so, do you see a way to put a "\" ahead of each "/" in $image ? (i can't use sed either, obviously), or is there a simpler way (i'm sure there is one >  )
any help is welcomed :=)
|
|
|
|
09-15-2006, 01:37 PM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,687
|
Hi,
You don't need to use / as a seperator for sed. It can be anything you want.
So instead of s/../../, try s%..%..%
Hope this helps.
|
|
|
|
09-15-2006, 01:47 PM
|
#3
|
|
Member
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Original Poster
Rep:
|
you are a god !
i've spent some hours with this problem, finding nothing on the net and with your answers it works great now
i saw nowhere that we could put other things than slashes  ....
are there other possibilities instead of "/" and "%" ?? (if one day i have a problem with "%" too 
|
|
|
|
09-15-2006, 01:57 PM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,687
|
Hi,
To my knowledge it can be any 'normal' character.
There's also the 'classical' approach: Escape all the special characters.
Code:
$ THISVAR="/foo/bar"
$ echo $THISVAR
/foo/bar
$ echo $THISVAR | sed 's/\//@/g' # using / as seperator. 'classic'
@foo@bar
$ echo $THISVAR | sed 's%/%@%g' # using % as seperator
@foo@bar
$ echo $THISVAR | sed 'sA/A@Ag' # using A as seperator
@foo@bar
Hope this clears things up.
|
|
|
|
09-15-2006, 02:23 PM
|
#5
|
|
Member
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Original Poster
Rep:
|
it is really clear now thanks to you
i didn't thought sed command would be this supple 
|
|
|
|
09-17-2006, 04:34 AM
|
#6
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
The pipe '|' is a good substitute for '/'. Makes sed more readable IMO.
sed -e 's|/some/file/path|/new/file/path|g'
As opposed to this:
sed -e 's/\/some\/file\/path/\/new\/file\/path/g'
Last edited by /bin/bash; 09-18-2006 at 07:05 PM.
|
|
|
|
09-18-2006, 02:17 PM
|
#7
|
|
Member
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249
Rep:
|
Indeed you can use any character instead of /. From the sed info manual:
Quote:
The syntax of the `s' (as in substitute) command is
`s/REGEXP/REPLACEMENT/FLAGS'. The `/' characters may be uniformly
replaced by any other single character within any given `s' command.
The `/' character (or whatever other character is used in its stead)
can appear in the REGEXP or REPLACEMENT only if it is preceded by a `\'
character.
|
Note that you can use _any_ character, even control characters (except nul), though I can't think why you'd want to.
P.S. I often use a comma as the small size is unobtrusive (and easy to type).
Last edited by soggycornflake; 09-18-2006 at 02:20 PM.
|
|
|
|
09-19-2006, 05:14 AM
|
#8
|
|
Member
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Original Poster
Rep:
|
thanks, a coma or more especially a pipe are good ideas as it is generally not seen in what comes afterwards
i think i'll use the pipe more, because there is very little chance to find it in what comes afterwards, while there may be a coma if it is a phrase 
but anyway it is very nice to see all these opportunities, and i don't understand why they preconize slashes by default, i lost some hours on this the other day lol
but yeah, thanks, it's all good now
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:28 AM.
|
|
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
|
|