LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-15-2006, 01:34 PM   #1
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
Question 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 :=)
 
Old 09-15-2006, 01:37 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-15-2006, 01:47 PM   #3
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 09-15-2006, 01:57 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-15-2006, 02:23 PM   #5
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Original Poster
Rep: Reputation: 15
it is really clear now thanks to you

i didn't thought sed command would be this supple
 
Old 09-17-2006, 04:34 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 09-18-2006, 02:17 PM   #7
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
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.
 
Old 09-19-2006, 05:14 AM   #8
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
  


Reply

Tags
sed


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
Sed shell script question pete1234 Programming 4 08-18-2006 09:15 PM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM
SED and shell variables 3saul Linux - Software 4 12-28-2005 11:37 PM
shell command help, sed i think infamous41md Programming 2 08-15-2004 02:55 PM


All times are GMT -5. The time now is 11:42 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration