sed help

I hear you!

We just got wifi and I’m in the garage listening to music and realized that I could go on the internet also. It’s a distraction (or option) that needs to be tamed.

Well, enjoy your wifi, I have been reading something insanely boring today, now I go out, and enjoy the rest of the evening. Have a still good day.

This is good for up to 999 lines:

set cmd to "<~/Desktop/WolfandCrane.txt  sed -En '
1 {
	# Before editing the first line, create a counter and put it in the hold space. The 30 digits are notionally the three rotating wheels of a three-digit meter, the first, eleventh, and twenty-first digits being the “visible” ones. The initial reading is 001.
# Append each line in turn to the meter in the hold space and copy the result into the pattern space.
# Lose the non-visible digits in the pattern space and substitute a dot and a tab for the linefeed between the meter and the line. (We know the first 30 characters are digits, so we can use “.” rather than “[0-9]” in the regex.)
# Lose any leading zeros and print.
# Get the original meter/line combination again from the hold space.
# Rotate the “units wheel” by one digit and lose the line text.
/^.{20}0/ {
	# If the units have returned to zero, advance the “tens wheel”.
	# And if the tens return to zero, advance the “hundreds wheel”.
	/^.{10}0/ s/^(.)(.{9})/\\2\\1/
# Put the advanced meter back in the hold space.
do shell script cmd

Text encoding for smart double-quotes corrected 2022-12-01.


I must say i am glad I didn’t try to write a proof or something, late last night, when I realized, I might have used the ‘y’ command to shift the numbers one place to the right, but I had trouble with the 100’s and 10’s.

Wery well done Nigel!

Don’t be put off that there aren’t much room in your sed script for doing any other text transforms. it was really instructive to read, and see a clever solution. :slight_smile:

Actually, this one will be plastered up on the wall!

Hi Nigel,

Wow! :cool: Great idea using the counter also! Who said sed can’t count, huh? :smiley:

Thanks a lot,

I did :slight_smile:

And it doesn’t, but who cares, as the effect is the same anyway!

It is going up on the wall.Nigel’s Fame, my Shame. :smiley:

I actually got some of the same idea last night, but I couldn’t get longer than to 9 lines.

I don’t remember you saying that. I was just using a bit of irony. :slight_smile: Yes, it is a great script!

BTW, does anyone know the maximum number of bytes ‘do shell script’ can handle?

Edited: I should say, from a file for processing.


It is really great! :slight_smile:

Should give you the maximum length of a command line a shell can handle, as for a do shell script command line, I’d guess that if there is a limit as to the size of the command line, that limit would be 2^14 characters (not bytes).

I guess stream input to a do shell script would be pretty much unlimited.

Thanks, guys. :slight_smile:

It turns out that one of the ‘h’ commands can be cut from my script and two of the 's’s combined if the meter reading starts at 001 instead of 000 and is incremented after each line is printed instead of before it. I’ve updated the script accordingly.

Eliminating the anomalies, I get 0.009999990463 secs… I have a feeling that this is good for four ling lines.

Agent Smith? :wink: