sed

Associate
Joined
1 Aug 2003
Posts
1,053
I am trying to use the sed command but my version seems to be different to the one described in books or online.

I am using debian and want to add some text to the begining of each line of a text file. Both i and a switches add new lines rather than editing current ones and my version doesn't seem to understand the s switch...

Any advice?
 
man sed? (I'm not being facetious, Im just interested as to whether the man page lists different values for the command-line switches). Also what does "sed --version" give you? I get (on Ubuntu 8.04) sed --version
GNU sed version 4.1.5

Finally what sed recipe are you using in case there's a logic error in it?
 
don't need quotes if there are no shell-interpreted characters, and the /g modifier is _per_ line - theres only one ^ meta character per line :p
 
Version is 4.1.2

I have used various versions to try and get something working, due to being a little frustrated I began just random rubbish towards the end (at least it looks like that now but it seemed to make sense at the time).

The command sed i\ mogrify\ quality\ 75 will add lines but not add to a line.

The command sed -i s/^/texttoadd/ is not understood but change the '-i' to 'i\' and it will add s/^/texttoadd to a new line NOT append current lines
 
Code:
$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
$ echo -ne "a\nb\nc\n" > textfile
$ sed -i s/^/texttoadd/ textfile
$ cat textfile
texttoadda
texttoaddb
texttoaddc

This doesn't happen for you? Weird sed :confused:
 
Ah, thank God someone said that rather than 'You massive tard!' - I was worried...

My sed is 4.1.2 - I could try updating sed and see where that gets me.
 
Grrr... now apt-get is playing up!

My sources seem to be invalid - I've got to go and hunt for some new ones, any suggestions or will I have to rely on Mr. Google, my special friend :)
 
Right.... sed seems to be behaving itself (have no idea why the version I was using was so out of date to the point of completely different command sets and rules).

Now I have the problem of Linux not understanding my commands - I think this is due to the obtusely complex file names that people decide to use.

With the command

mogrify -quality 75 /usr/data/PROJECTS/2933 Marshall St 2007/Images/D4325_0001.Foundation_(1024_x_768).jpg

It seems to get lost at the brackets, I have my ideas but would welcome suggestions...
 
Hmm, I think someone will call me a moron for not knowing how to do this but... how do I get sed to tack a ' at the end of every line.

To get round the special characters that people use, I'm putting the filenames in quotes
 
In trying to put quotes around the file names, I'm using two expressions, one to tack onto the front of the file name (which works) and one to tack on to the end... which I'm having problems with..

sed -i 's/$/\'/g'

It seems to get hung up on ', the \ is there as an escape character but doesn't work... any suggestions?
 
why are you trying to put quotes arround the filenames yourself? is this from within a script?

to handle spaces and special chars in a script, the following will work :
Code:
IFS="
"
for i in /path/to/files/*;do
mogrify -quality 75 "$i"
done

But to put a single quote on the end of a string:

Code:
$ echo test|sed s/$/\'/
test'
or
Code:
$ echo test|sed "s/$/'/"
test'
or
Code:
$ echo test|sed 's/$/'\''/'
test'

take yer pick :D
 
Last edited:
The root of the filesystem. It is a path.

That's a common way for a command or program to interpret a string starting with a / character, but that doesn't mean it's a shell-interpreted character.

what do you think a shell does for this? "echo test|sed /test/d"
 
Back
Top Bottom