top | item 17607291

(no title)

jssfr | 7 years ago

Author of xmpp-echo-bot here.

> echoz.sh also uses cut, base64, stdbuf, tr, mkfifo, tee and sort.

tee can be eliminated if needed be, the others should probably be added to the list of dependencies, thanks.

> echoz.sh does not seem to have any "bashisms" so perhaps one does not actually need bash and other sh will suffice.

Indeed, there is a PR which I have to review which fixes that.

> The uses of cut to isolate substrings and tr to replace characters can also be acomplished using sed.

I would be interested how you would do especially the tr part in sed, given that sed, even unbufferedly, operates on "lines". I use tr to replace '>' (XML delimiter) with a line ending recognized by sed to be able to operate on individual XML elements.

The uses of cut could be replaced by sed in the script indeed, although it can’t be moved into the main sed script. I made an issue for that: https://github.com/horazont/xmpp-echo-bot/issues/6

<spoiler>

> "All you have is openssl, bash, dig, stdbuf and sed?"

I hope it is clear that this entire thing is ironic and the long list of dependencies -- especially given that bash could reasonably be used to replace sed here -- is supposed to underline that. I’ll gladly add more :).

</spoiler>

discuss

order

textmode|7 years ago

"I would be interested in how you would do especially the tr part in sed..."

There is more than one tr part. :) I tried to be careful to refer only to where the task is replacing characters.

As for joining lines, I have used the same tr -d '\12' technique quite often in the past if the input is not contained in a file. For files I would sometimes use ed scripts.

IME, sed is one of the most ubiquitous programs in UNIX-like OS. I use it daily. However when sed cannot do the job or cannot do it fast enough, I use flex to write relatively small, single-purpose programs quickly. There is a good chance they will compile cleanly on a variety of OS. Perhaps flex is as available to the user as tr, mkfifo, openssl, etc., i.e., when the user has access to all those programs she also probably has access to flex, cpp, as, ld and cc.

I would be happy to provide an example directed at the xmpp-echo-bot solution if you can provide some sample input and the desired output, ICPC-style.

textmode|7 years ago

In processing HTML, XML or JSON with sed I have often used tr (e.g., delete newlines, add non-printable delimiter, then replace delimiter with newline) to reformat into sed-friendly input. However, an easy alternative to using tr for this is flex.

As an example, below is a one-off/reusable HTML/XML reformatter in flex. This makes HTML/XML easier for me to read. It also makes it very easy to process with sed and other line-based utilities.

    ftp -4o 1.xml http://web.archive.org/web/20130814000845/http://zombofant.net/blog/ |a.out |less

    flex -8iCrfa 038.l 
    cc -static lex.yy.c

    cat 038.l

    #define echo ECHO
    #define jmp BEGIN
    #define nl putchar(10)
    #define ind fputs("\40\40\40",stdout)
   %s xa xb 
   xa \11|\40 
   %%
   ^\x0d\x0a jmp xb;
   \<{xa}*script nl;ind;echo;jmp xa;
   <xa>\<{xa}*\/script{xa}*\> echo;jmp xb;
   <xa>{xa}{xa}* putchar(32);
   <xa>. echo;
   <xb>\< nl;ind;echo;
   <xb>\> echo;
   <xb>{xa}{xa}* putchar(32);
   <xb>. echo;
   .|\n
   %%
   int main(){ yylex();}
   int yywrap(){ nl;}