bernsten@phoenix.Princeton.EDU (Dan Bernstein) (03/15/89)
In article <18706@adm.BRL.MIL> rbj@nav.icst.nbs.gov (Root Boy Jim) writes: > sed -e '1{;h;d;}' -e '$!{;G;h;d;}' -e '$G' As you point out, this is limited by sed's buffer limit of 4096 characters. > An awk script would be a better way to do this. I hate awk, and nobody else has presented an ed version, so here goes: #!/bin/sh # Reverse the lines of the input. Missing final newline deletes last line. cat $* > /tmp/tac$$ ed - /tmp/tac$$ << EDCMDS 1,\$g/.*/m0 1,\$p EDCMDS rm -f /tmp/tac$$ > Tail -r seems to be the best buggestion On this system, tail's buffer is 4096 characters, not much bigger than sed's. > unless you are reversing > really BIG files. In that case you probably do want to write a C > program. There's just no escaping the buffering problem. If you don't feel like writing or procuring a C version of tac, try the ed script above. It does a perfectly good job of reversing many-megabyte files. The only real problem with using these built-in utilities is that they can't handle a missing final newline. If the final line lacks a newline, all the examples given so far will omit that line. When your input is short enough to use tail -r, you're safe because tail -r adds an extra newline---but that means it's not truly reversible. On the other hand, if all you care about is getting the lines reversed and the number of extra newlines is irrelevant, just add an echo '' >> /tmp/tac$$ to the ed script above and you're home free. ---Dan Bernstein, bernsten@phoenix.princeton.edu
wisner@shadooby.cc.umich.edu (Bill Wisner) (03/15/89)
Note that GNU tail (there really is such a beast) has no stupid, petty, small buffer length limit.