On 2022-01-02 at 18:40, Greg Wooledge wrote: > On Sun, Jan 02, 2022 at 06:20:15PM -0500, The Wanderer wrote: > >> If you mean that you want to only replace the first matching line >> in the file, but leave any subsequent matching lines alone... I've >> never actually had occasion to do that, but a bit of Googling (for >> 'sed first match only') found me >> >> https://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file >> >> which looks like at least the seed of a way to do it; see also >> below. > > Why does everyone think sed is an appropriate tool for this? It's > some sort of malevolent meme, I swear. Probably, at least in part, because it's the tool that can potentially do it which people are already most familiar with based on having encountered / used it elsewhere. In this specific case, I stuck with it not only because of that but also because it was the tool the original inquirer was already using. > unicorn:~$ printf %s\\n foo foo foo | awk '!done && /foo/{$0="bar"; done=1} 1' > bar > foo > foo I keep forgetting that printf is even a thing in shell, and while awk did occur to my mind, I don't know it well enough to do anything useful in it. I can only kind-of parse the syntax you gave there, and that only because I just saw very similar-looking syntax given for sed in the Stack Overflow answer linked above. > Of course, given the ORIGINAL question, the best tool is neither of > these. It's something like ansible or puppet. Or if you insist on > jury-rigging crap together with stone knives and bearskins, ssh and > patch. I'm not sure what original question you're referring to; I infer that it's probably going to be something that predates this thread, because the one that opened this thread doesn't seem to have any nexus to either of that first pair of tools. I can kind of see how patch (and, if applicable for working with things on a remote machine, ssh) could be used for something like this, but only in limited contexts. For the biggest issue, AFAIK and AFAICT patch is line-based, and I infer from the post which started this thread (specifically the fact that the search pattern does not consist of the entire line) that the full line which needs to be replaced is not known. If patch provides any facility which would let it be usable in that case, I'm not seeing it. (Unless maybe you provide the patch in the form of an ed script? I don't know whether ed has the breadth of features to make this doable either, but it seems likely. I didn't even remember that ed scripts were valid patch format until rereading the man page just now.) > See also > <http://backreference.org/2011/01/29/in-place-editing-of-files/>. That's very interesting, although not all that accessible to the relative newcomer to the field. It does leave me sad about the apparent conclusion that there is no safe way to edit a file programmatically (i.e., not via an interactive editor, and possibly not at all) in a way which preserves the inode; the reasons why I thought of that as desirable seem less clearly so on further analysis, but it's still unfortunate for a thing to not be possible to do. -- The Wanderer The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man. -- George Bernard Shaw
Attachment:
signature.asc
Description: OpenPGP digital signature