help with sed please

Status
Not open for further replies.

sigmend

[H]ard|Gawd
Joined
Aug 6, 2003
Messages
1,303
I have a file containing the line
Code:
<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>
This is what I have so far, as you can prolly tell, I do not know how to use sed
Code:
sed "s/<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>//" < com.apple.systemuiserver.plist >> com.apple.systemuiserver.plist
I want to remove it with sed, but I have a problem where it takes /L as an argument or something.
So, my question is, what is the correct way to use sed?
 
Originally posted by sigmend
I have a file containing the line
Code:
<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>
This is what I have so far, as you can prolly tell, I do not know how to use sed
Code:
sed "s/<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>//" < com.apple.systemuiserver.plist >> com.apple.systemuiserver.plist
I want to remove it with sed, but I have a problem where it takes /L as an argument or something.
So, my question is, what is the correct way to use sed?

basically, s<character>search regex<same character>replacement string<same character>

for search and replace.

I'm not testing this, so it may or may not work, but it'll be closer than your attempt.

"s+<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>++"

Try not to use a separator that is contained in the string you're trying to replace, it'll make your life a bit easier. IIRC, you can do it with escaping, but it's much clearer if you don't. You may have to escape the < and > characters with a \.
 
Originally posted by FuzzyDonkey
basically, s<character>search regex<same character>replacement string<same character>

for search and replace.

I'm not testing this, so it may or may not work, but it'll be closer than your attempt.

"s+<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>++"

Try not to use a separator that is contained in the string you're trying to replace, it'll make your life a bit easier. IIRC, you can do it with escaping, but it's much clearer if you don't. You may have to escape the < and > characters with a \.


if you're going to backtick the front-slashes (like \/), then you can just do it with the normal /

like this:

s/<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>//

The extra \ is just to protect the / from being seen by the regex as a part of the syntax.
 
Originally posted by fat-tony
if you're going to backtick the front-slashes (like \/), then you can just do it with the normal /

like this:

s/<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>//

The extra \ is just to protect the / from being seen by the regex as a part of the syntax.

Yea... been a while since I had to use it regularly.
 
if you only want to remove the one line, why not just use grep?

grep -v spits out all lines that don't contain your string.
 
I am still confused
This is what I have so far

This one I attempt to use grep -v Which I can not seem to get to work
Code:
find /Users -name "com.apple.systemuiserver.plist" -exec "grep -v '<string>/System/Library/CoreServices/Menu Extras/iChat.menu</\string>' < {} >> {}" \;
with this one I get the error
Code:
find: grep -v '<string>/System/Library/CoreServices/Menu Extras/iChat.menu</\string>' < com.apple.systemuiserver.plist >> com.apple.systemuiserver.plist: No such file or directory

And this one where I use sed . Cant seem to get this one to work either
Code:
find /Users -name "com.apple.systemuiserver.plist" -exec sed s/\<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>// < {} >> {}
with this one I get the error
Code:
./test2: line 2: /System/Library/CoreServices/Menu/Extras/iChat.menu: No such file or directory
I just need one of these to work, and I am not having any luck with either.
and the man pages confuse me

help again :(
 
Are you sure that

cmd < file >> file

is what you really want to do? I don't think it is...


In the case of sed, you don't have the sed command-line quoted, so find is trying to interpret the sed command as something for it.



...and it appears that OSX find is funny, try using /Users/. On the machine here find /dirname just spits out the dirname without recursing, find /dirname/ recurses

don't ask me why.
 
If I have a trailing slash after users, it will output like /Users//userdir/Library/...


Now, with the quotes around sed, it gives me a longer error
Code:
#!/bin/bash
find /Users -name "com.apple.systemuiserver.plist" -exec "sed s/\<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>// < {} >> {}" \;
Outputs
Code:
find: sed s/\<string>\/System\/Library\/CoreServices\/Menu\/Extras\/iChat\.menu<\/string>// < /Users/mckaym/Library/Preferences/com.apple.systemuiserver.plist >> /Users/mckaym/Library/Preferences/com.apple.systemuiserver.plist: No such file or directory

I prolly am going about this all wrong.
Maybe some one will know a more efficient way, so let me explain what I am trying to do.

I need to find a file called "com.apple.systemuiserver.plist" in a users home directory, then I need to remove the line
Code:
		<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>
from it.
My initial thought was to use sed, but I don't know what the hell I am doing.
 
One of my find invocations goes like:

find -name "*.exe" -type f -exec strip -s {} ";"

I suspect that {} must be a separate argument to find.

Try this:

Code:
[4/16/2004 Fri 6:16.16 AM stl@nuwen ~/foo] [493 490 983]
> ls
foo.txt  oink.mlar  oink.txt

[4/16/2004 Fri 6:16.17 AM stl@nuwen ~/foo] [493 490 983]
> cat foo.txt
alice
betty
carol
diana

[4/16/2004 Fri 6:16.18 AM stl@nuwen ~/foo] [493 490 983]
> cat oink.mlar
alan
bjarne
baz
charles
baz
donald

[4/16/2004 Fri 6:16.20 AM stl@nuwen ~/foo] [493 490 983]
> cat oink.txt
foo
bar
baz
quux

[4/16/2004 Fri 6:16.21 AM stl@nuwen ~/foo] [493 490 983]
> find . -name "*.txt" -type f -exec sed -i s/baz// {} ";"

[4/16/2004 Fri 6:17.15 AM stl@nuwen ~/foo] [493 490 983]
> cat foo.txt
alice
betty
carol
diana

[4/16/2004 Fri 6:17.17 AM stl@nuwen ~/foo] [493 490 983]
> cat oink.mlar
alan
bjarne
baz
charles
baz
donald

[4/16/2004 Fri 6:17.20 AM stl@nuwen ~/foo] [493 490 983]
> cat oink.txt
foo
bar

quux

[4/16/2004 Fri 6:17.22 AM stl@nuwen ~/foo] [493 490 983]
>
 
Like amoeba said, I really doubt that you want to do anything like "grep -v 'foo' {} >> {}". ">>" means "append", so every changed file would consist of the original contents of the file followed by the original contents minus lines matching the expression you passed to grep -v.

If you are still having trouble, maybe try something like this:
Code:
#!/bin/sh

# So filenames with spaces don't turn into multiple arguments:
IFS=''

for file in `find /Users/ -name com.apple.systemuiserver.plist -print`;do
    mv $file ${file}.bak &&
    grep -v '<string>/System/Library/CoreServices/Menu Extras/iChat.menu</string>' ${file}.bak > $file
done
Save that as a shell script and run it, or paste it in minus the bang and comment lines at some variety of sh (not a csh). If that works, then you can later use find to '-exec rm {} \;' (which shouldn't be problematic) to get rid of the backups.
 
I already solved the problem; use sed -i for in-place editing.

I bet sed < file > file would work, but I don't know yet how to convince find to do that.
 
Status
Not open for further replies.
Back
Top