In the post Sed - Replace first k instances of a word in a file, a user suggests:sed 's/old/new/;s/old/new/;s/old/new/;s/old/new/;s/old/new/' filenameas a method of replacing the first k instances of a word in a line. Seems like an inelegant solution to the problem of replacing the first k instances of old with new. There are other methods discussed, to do other things. But there are no other (or no other elegant) solutions to the problem of replacing the first k occurences of a word in a line using sed.tl/dr:I have a large group of items I call slugs that are really helpful for a variety of odd little things that I want to keep track of. They look like this:#slug YYYY/MM/DD d hhmm field1 field2 field3 notesAnd I keep them in a series of files that are dated. Then I put the files through a grep | sed | awk chain to generate reports from the data. So let's say my neighbor's dog is barking and it's keeping me up at night. Every time I hear the dog bark I will write down the time, and later I will note it in my logfile.#bark 2016/04/06 W 0214 0232 Police showed up!And after a while, I will have a database stored up, and hopefully, I will notice some trend, like... who knows what... the dogs bark at the full moon or something. The chain of little programs would look like:grep ^#[Bb][Aa] 2014* 2015* 2016* |\sed 's/ /|/;s/ /|/;s/ /|/;s/ /|/;s/ /|/' |\awk {something or other} >\barking-dog-reportSo, my problem is the sed portion:sed 's/ /|/;s/ /|/;s/ /|/;s/ /|/;s/ /|/'Which translates here into replace the first five spaces into pipes. I use the pipe as the field separator in awk. A command like s/ /|/5 doesn't work, that just changes the fifth space on every line into a pipe. I just think that series of sed commands looks ridiculous. It's not a global replace s/ /|/g, because I want to retain the spaces as spaces (and not use them as field separators) for the notes section.
Better way to run a series of seven identical commands in sed?
sed;awk;grep