Click to See Complete Forum and Search --> : sed - loops
bryan.6
10-15-2003, 12:52 PM
do all loops have to be in a script file? all of the examples i have seen have been in sed script files, and i haven't been able to get mine to work on the command line. does anyone have an example of one that works on the command line?
bwkaz
10-15-2003, 06:36 PM
What do they look like in the script file?
My guess is that you're running into quoting issues, but I'd have to see what a sed loop looks like to know for sure. I suppose I could just read info sed, but I'm lazy. ;)
bryan.6
10-15-2003, 09:52 PM
it usually looks something like this, if i can remember right
:x
s/// #some sort of substitution, maybe some other stuff
tx
i tried using curly braces ie sed -e "{:x; s///; tx}", just semi colons (is this even what i want to be using?) sed -e ":x; s///; tx" and i think some other stuff. yes i realize that the s/// isn't doing anything, but i'm away from my computer, and i don't remember what it was, but i do remember that the substitution was working (which is a requirement for the loop to work with 't').
bwkaz
10-16-2003, 06:21 PM
I don't know what the sed command separator is, but I'd bet that it's not semicolon. ;)
Try something like this:
sed -e ':x
s///
tx' After you hit return after the :x, you'll get a different prompt, most likely just a right angle bracket ('>') symbol. This is because you hit return without ending the single quote pair.
If you do it this way, sed will get the explicit newlines in the script, and it should interpret those the same as it would in a separate file.
andysimmons
10-18-2003, 06:42 PM
Originally posted by bwkaz
I don't know what the sed command separator is, but I'd bet that it's not semicolon. ;)
I'll take that bet!!!
You can actually use whatever you want for the separator, with the exception of whatever you used to enclose the command (i.e. quotes or ticks).
andysimmons
10-18-2003, 06:53 PM
Oh yeah...back to helping the person who asked for help in the first place...
What are you trying to do that requires multiple lines? I've never used a multiline sed command, unless you mean multiple sed commands in which case you can separate them with -e. Try something like this to see what i mean:
echo sed sucks | sed -e "s/su/ro/" -e "s/$/ my face off!/"
That'll take the string "sed sucks" and replace su with ro, then take the new string "sed rocks" and change the end of the line to " my face off!"
Is that what along the lines of what you're trying to do?
bryan.6
10-18-2003, 07:48 PM
i'm trying to replace all of the hyphens with space-hyphen-space, except when they have spaces already. so i really can't just find a letter or number preceeding a hyphen, and then change ALL of the hyphens on that line, because the second hyphen might already have spaces. so that's why i want the loop, or is there a better way to go about this?
bwkaz
10-18-2003, 08:33 PM
sed -e 's/([^[:space:]])-([^[:space:]])/\1 - \2/g' <file >file.new
will accomplish most of that nicely, unless you have any instances where there's a space before the hyphen but not after (or after but not before)...
It looks for hyphens, surrounded by a character that's anything except a whitespace character (space, tab, and newline are the default whitespace characters). It then puts whatever characters did surround the hyphen, into the groups \1 and \2, so it can use them in the replacement. It then replaces the whole string with "\1 - \2" (so the surrounding characters are not destroyed).
The g at the end means "replace all matches on all lines" instead of "replace the first match on all lines", which is the default.
bryan.6
10-18-2003, 09:10 PM
when i ran this, it said it was an invalid reference to \2.
with some experimentation, i figured out it probably should be \0 and \1, right? anyway, this doesn't work for me either. i tried to simplify it to just make the first space, and this finally worked, sortof.
music$ ls -1 *.mp3
01-band-title.mp3
02-band-another_title.mp3
03-band-and_so_forth.mp3
music$ ls -1 *.mp3 | sed -e "s/[^[:space:]]-/\0 -/g"
01- -band- -title.mp3
02- -band- -another_title.mp3
03- -band- -and_so_forth.mp3
so i assume that \0 isn't just taking the value of [^[:space:]], but instead takes the value of [^[:space:]]-, which would also explain why it wouldn't resolve \1 when i was tryin with both spaces.
bwkaz
10-19-2003, 01:59 PM
\0 resolves to "the entire string that was matched".
\1 (and \2) are "the first (and second) substrings that were matched", where a "substring" is defined as a section of your regular expression that was enclosed in parentheses.
You did include the parentheses, right? Both sets of them? Because your example doesn't have any...
bryan.6
10-20-2003, 12:27 PM
no, i guess i don't think i did include them. are they some special character so they don't get searched for, or what? anyway, i'll try that next when i get my linux system back up and running.
bwkaz
10-20-2003, 06:25 PM
Correct, they don't get searched for.
(Unless you escape them, that is, like this: "\)".)