« first day (239 days earlier)      last day (2160 days later) » 

8:43 AM
@EliahKagan I started out writing (or planning to write) a lot of information, and then I decided it wasn't to the point and tried to say as little as possible. Maybe linking to something would help there...
today I feel even sleepier than yesterday :(
@EliahKagan now I remember that I was kind of hoping you would tell me some more about this topic after I wrote that answer. I didn't feel like asking you about it... I read that the operating system calls the interpreter for a script with a shebang line, or if that's not possible Bash will do it... I wondered about the exact details of this, and I remembered you saying this which was helpful... So thanks for clarifying that.
I guess I should just remove the part in parenthesis there
 
 
2 hours later…
 
1 hour later…
11:59 AM
@Zanna Can you tell what they're trying to do by viewing the source? They've enclosed their command in <code> </code> tags, so I'm not sure if it's displaying what they want to display, or if they actually want to show the doubled backslashes they've typed, or what. You know sed a lot better than I do... any guesses?
 
 
2 hours later…
2:13 PM
@EliahKagan aah good thought to check that! I guess they did want to show the double backslashes... but still... can't figure out what that command with no commands is trying to do
 
115 messages moved from Raiders of the Lost Downboat
 
2:31 PM
@Zanna Maybe they worked in something in the terminal for a while, got it to a point where it worked or where they thought it worked, and then attempted to manually type it in, did so incorrectly, and then made who knows what other changes to it while fighting the Markdown?
*worked on something
 
2:49 PM
@EliahKagan yes, I assume something like that... they seem to think the backslashes at the end were the magic fix... but why do they think so?
zanna@toaster:~/playground$ echo Hello World > mytext.txt
zanna@toaster:~/playground$ sed -r a"Hello \\ World \\" mytext.txt
Hello World
Hello \ World
zanna@toaster:~/playground$ sed -r a"Hello \\ World" mytext.txt
Hello World
Hello  World
it's the shell...
zanna@toaster:~/playground$ sed 'aHello \\ World' mytext.txt
Hello World
Hello \ World
Bash removed one backslash... sed got one backslash and shrugged
o.O
I still don't get it :D
something to do with the last quote being escaped in sed
but it won't see that...
 
3:22 PM
@Zanna I've tried my hand at editing that answer.
 
just reading the revision now. Thank you very much!
looks awesome! thanks for all the links :D
 
No problem. Most of them are from the bash reference manual page, which you'd already linked to, so it was quick for me to get them.
 
I should try to add more references when writing answers. Hyperlinks take a lot of the pain out of that job
 
3:56 PM
Hmm, my answer there has no references at all, nor links of any kind...
 
haha that's ok
 
@Zanna How? Bash doesn't treat backslashes specially in single-quoted strings.
 
@EliahKagan I mean, OP used double quotes
 
@Zanna I thought you were talking about the behavior with single quotes that you showed there.
 
When I use single quotes, the expected thing happens... sed prints one backslash because it treats backslashes specially
 
4:01 PM
Yeah.
 
but with the double quotes, the two backslashes at the end... are needed to get the backslash in the middle
 
...would it benefit from some?
 
I can't figure out what's happening with with final backslashes, but without them, one of the ones in the middle gets removed by Bash (right?) and so sed gets one, which it doesn't print
 
The final backslashes in which command?
 
sorry for the confusing commands
zanna@toaster:~/playground$ echo '.' | sed "aHello \\ World"
.
Hello  World
zanna@toaster:~/playground$ echo '.' | sed "aHello \\ World\\"
.
Hello \ World
(the a and i commands don't work on empty files, annoyingly)
not that I am aware of... I think you explained all the things that needed explaining there
 
4:29 PM
$ echo ' ' | sed "s/ / \\ /; \\"
sed: -e expression #1, char 11: unterminated address regex
 
4:43 PM
@Zanna that's interesting... sed sees the last backslash and thinks it's the start of an address regex. You can use an alternative (alternative to /) character to indicate/delimit a regex address if you escape it first
$ cat << EOF | sed '\#foo# d'
> hi
> foo
> bar
> EOF
hi
bar
but the a and i commands take everything after them to be the line to be printed... and they don't take addresses
ok I'll think aloud elsewhere :D
 
So you figured it out?
 
no. have you?
 
No.
 
I don't know what voodoo that trailing backslash can do to the one in the middle. And I don't know what command OP used because... they didn't show any XD
 
5:20 PM
@Zanna of course they take addresses. but you can't put anything after the line... there's no way to indicate you've finished the line... everything gets printed literally...
zanna@toaster:~/playground$ echo '' | sed "c.\\\\"
.\
zanna@toaster:~/playground$ echo '' | sed "c.\\\\more"
.\more
zanna@toaster:~/playground$ echo '' | sed "c.\\\\"
.\
zanna@toaster:~/playground$ echo '' | sed "c.\\more\\"
.\more
zanna@toaster:~/playground$ echo '' | sed "c.\\more\\more"
.moremore
zanna@toaster:~/playground$ echo '' | sed "c.\\more\\more\\"
.\more\more
o.O
 
6:20 PM
@Zanna They don't?
Shouldn't they append or insert for each line, such that the correct ("working") behavior is to do nothing when the input is empty?
 
yes
makes sense
 
I'm confused. I don't know what we're trying to figure out, anymore, or how it relates to that post. I am actually pretty weak when it comes to sed.
 
it's ok. sorry for all the posts
I will move them
 
@Zanna Sorry, I mean, I'm hoping you can walk me through it. I'm not sure if that should happen here or on the island. And you don't have to explain it if you don't want to. It's up to you. I can move them later if you like. If we come up with something then it might affect the post and that would determine how severable the technical discussion ends up being from the issue of what to do with that question and its self-answer. :)
 
6:36 PM
I don't understand it myself so far :(
But, the OP of that question somehow found, I am guessing, that putting two backslashes at the end of the string allowed them to get one printed in the middle of the string...
they could have just quoted their sed command properly, but they found another, strange way of doing it
They wanted Hello \ World which can be easily made like that:
$ echo '' > file
$ sed 'cHello \\ World' file
Hello \ World
but cannot be made like this
$ sed "cHello \\ World" file
Hello  World
I think I understand that... in double quotes, backslashes escape the next character. The first backslash gets taken away by the shell. Sed doesn't print the one backslash it saw, because it also treats a backslash as an escape (but spaces don't need to be escaped - they are not special in Sed (are they?))
however, strangely, this does work:
$ sed "cHello \\ World\\" file
Hello \ World
So, OP apparently found something interesting
I don't know why that happens.
What does the last backslash signify?
This only seems to work with the a c and i commands
whatever comes after those commands in general gets printed. But it seems there are some exceptions. For one thing, to make it more readable, you conventionally put a backslash before the line you want to insert...
Sed only complains about the missing backslash if there are no non-whitespace characters after the command:
zanna@toaster:~/playground$ sed "c" file
sed: -e expression #1, char 1: expected \ after `a', `c' or `i'
zanna@toaster:~/playground$ sed "c " file
sed: -e expression #1, char 2: expected \ after `a', `c' or `i'
OP's magic doesn't work, if there are no non-whitespace characters before the backslash they want to print, unless there's a backslash after the sed command...
zanna@toaster:~/playground$ sed "c\\\\" file

zanna@toaster:~/playground$ sed "c\ \\\\" file
 \
zanna@toaster:~/playground$ sed "c.\\\\" file
.\
so, after one of these commands, when generally everything you write gets printed, backslashes are still doing special things I don't understand
they don't even have to be at the end!
zanna@toaster:~/playground$ sed "c.\\more\\" file
.\more
zanna@toaster:~/playground$ sed "c.\\\\more" file
.\more
I suspect those two work for different reasons... in the first case, OP's voodoo. In the second, sed saw two backslashes so printed one, as with 'c.\\more'
@Zanna same thing there, that's not weird, is it? four backslashes in double quotes = 2 backslashes in sed = 1 backslash to stdout
but if you put something in between the two sets of backslashes you get the same result... so it's as if the backslashes in the middle get quoted by the implicit or explicit one at the beginning, and the explicit one at the end
 
7:19 PM
So, we are assuming they ran this literally, rather than what it currently displays as in the post, right?
sed -i -r "Hello \\ World \\" mytext.txt
Because this is just the beginning fragment of a command, not an actual command, because the last backslash escapes the double quotation mark and prevents the quotation from being closed, right?
sed -i -r "Hello \ World \" mytext.txt
@Zanna So we can write that this way to make it easier to think about (and because this is the way we would ordinarily write it), right?
sed 'cHello \ World\' file
 
@EliahKagan yes
@EliahKagan oh good idea!
excellent
 
7:42 PM
According to the GNU sed reference manual, the a and a\ commands are separate, the unbackslashed a command being a GNU extension.
But it doesn't seem to explain this behavior of the a command.
 
it works the same for a and... the one with the backslash :)
 
7:57 PM
So this affects c, c\, a, a\, i, and i\ ?
@Zanna To write a\ in chat, use:
``a\``
(There we go.)
 
yes. A single backslash at the end (it needs to be exactly the last character before the closing quote) magically quotes any others in the middle
 
For exactly those three (or, uh, six) commands in sed and no others?
So also there is this:
$ sed 'c\Hello \ World' <<<.
Hello  World
$ sed --posix 'c\Hello \ World' <<<.
Hello  World
$ sed 'c\Hello \ World\' <<<.
Hello \ World
$ sed --posix 'c\Hello \ World\' <<<.
sed: -e expression #1, char 16: incomplete command
For the last one, the same thing happens if I have a literal newline after the first backslash:
$ sed --posix 'c\
> Hello \ World\' <<<.
sed: -e expression #1, char 17: incomplete command
In the GNU sed source code, compile.c from line 1150 seems relevant.
Unless you prefer to do it, I'm going to move the sed messages stemming from considering that post to the island, so we (and anyone else interested) can keep discussing them there. This is not getting any less technical or long. :)
 
oh thanks, please go ahead!
 
8:16 PM
95 messages moved from Raiders of the Lost Downboat
 
oh look a goto XD
 
Ooh, Launchpad lets you link to a line. This is a convenient way to go there in the code.
@Zanna Yeah, we should look at where else goes to that label.
 
    if (ch == '\\')
	    ch = inchar();
 
\ is special in C, too.
'\\' is a character literal for a backslash in C.
 
I don't understand the inchar() definition, but I assume it will help us
 
8:24 PM
Well the comment tells you what it does.
/* Read the next character from the program.  Return EOF if there isn't
   anything to read.  Keep cur_input.line up to date, so error messages
   can be meaningful. */
Oh, we linked to the same line.
Well, it consumes the next character from the sed program text.
As for how the implementation works, there's more than one way for a sed program to be provided to the sed interpreter, so it might already have it in memory and read the next character from within a data structure that holds it, or it might be reading it from a file and thus read a character from the file. The implementation covers both situations separately.
 
so, if there is a backslash...
hmm I need to go to sleep, or at least lie in bed counting backslashes jumping over a gate :(
 
:)
Before that point, disregarding (for now) ways it gets there via goto, ch got its value from the in_nonblank function, which consumes and discards blanks until it gets to a nonblank, which it returns.
So ch holds the most recent interesting character from the sed script. If it's a backslash, then the next character is read. What's more interesting is what happens when it's not a backslash.
(Btw you needn't respond. I'm not trying to keep you from sleep. These messages will still be here later.)
 

« first day (239 days earlier)      last day (2160 days later) »