[CONTACT]

[ABOUT]

[POLICY]

[ADVERTISE]

without The last post in

Found at: republic.circumlunar.space:70/~katolaz/phlog/20201009_fmt_without_fmt.txt

             fmt(1) without fmt(1)
             =====================

		
The last post in the "ed(1) is the right tool" series was about
justifying a text file using fold(1), and the fact that fold(1) does not
do "the right thing" with newline characters within paragraphs. The
solution based on manually "joining" lines in ed(1) was just there to
introduce the "j" command, but I admit it is relly unwieldy. 

		
So how you do what fmt(1) would do if fmt(1) is not there? Well, this
pipeline would do the magic (or at least most of it)(*):

		
  $ sed -E -n '/^$/!{H;};/$^/{p;x;s/\n/ /g;s/^ +//g;p;}' roophloch_2020.txt | \
    fold -w 66 -s

		
This is actually a combination of two commands. The first is an
invocation of sed(1), which is followed by some gibberish arguments that
might well have come directly from `cat /dev/random`. That stuff is
indeed a "sed program", and is what does the magic. The result of such
"magic" is then piped to fold(1). 

		
Obviously, I have no intention to leave the "sed program" above
unexplained. But one post would not be enough to get to the depth of
sed(1) programming. And since the "ed(1) is the right tool" series of
phlogs is coming to an end, I decided that a series will follow that one
pretty soon, and it will be called "sed(1) is your best shot".

		
For now, though, it will be enough to know that in sed(1)'s idiom the
string:

		
  '/^$/!{H;};/$^/{p;x;s/\n/ /g;s/^ +//g;p;}'

		
actually means:
  
   - store each input line in a temporary buffer, one after the other,
     until you find an empty line;

		
   - when you read an empty line: 

		
     - print the empty line

		
     - replace all the newlines in the buffer with a space;
    
     - remove all the spaces at the beginning of the buffer (if any);

		
     - print the buffer on output

		
Bottom line: fold(1) is enough to do most of the things that fmt(1)
would do, if sed(1) is willing to provide a helping hand. The plan is to
explore how powerful this helping hand might be.

		
 -+-+-+-+-

		
sed(1)  appeared in UNIX V7 (1979)

		
(*): The "sed program" was slightly modified after I published this 
     phlog to make sure it works on POSIX sed(1) (and on FreeBSD 
     sed(1) as well)


AD:

NEW PAGES:

[ODDNUGGET]

[GOPHER]