eeh@Dixie.Com (Ed Howland) (04/01/91)
This post might not belong here, but since there is no group for purely Awk programming, I though I might ask here. In the book "The Unix Programming Environment", by Kernighan and Pike, there is an excelent introduction to 'awk', which I learned to program in it. I was re-reading this the other day and came upon the question: "What is the shortest awk program that just copies its input to its output?" I came up with : !0 Is there anything shorter? Why does'nt just '1' (or any non-zero value) work? Also, does this hold true to perl? (Sorry I don't know perl well enough to to even guess.) What would be the shortest perl program to do this? Ed Howland : uucp: ..emory!rsiatl!eeh
jv@mh.nl (Johan Vromans) (04/01/91)
In article <9004@rsiatl.Dixie.Com> eeh@Dixie.Com (Ed Howland) writes: > "What is the shortest awk program that just copies > its input to its output?" I came up with : > > !0 > > What would be the shortest perl program to do this? % perl -pe < file If command-line options are not allowed: % perl -e 'print <>' < file Johan -- Johan Vromans jv@mh.nl via internet backbones Multihouse Automatisering bv uucp: ..!{uunet,hp4nl}!mh.nl!jv Doesburgweg 7, 2803 PL Gouda, The Netherlands phone/fax: +31 1820 62911/62500 ------------------------ "Arms are made for hugging" -------------------------
allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) (04/05/91)
As quoted from <9004@rsiatl.Dixie.Com> by eeh@Dixie.Com (Ed Howland): +--------------- | !0 | Is there anything shorter? Why does'nt just '1' (or any non-zero value) work? +--------------- It probably matches the line number, so a given positive number N would match only on line N. (??? I have never understood all of awk completely; the documentation for standard awk seems to leave a lot to be desired. The new awk and GNU awk may well be different there --- new awk has a book, at least.) +--------------- | Also, does this hold true to perl? (Sorry I don't know perl well enough to | to even guess.) What would be the shortest perl program to do this? +--------------- "perl -pe ''" is the shortest, but may not be fair because it uses command line arguments to "imply" a script. 'while(<>){print;}' is longer, as is 'print while$_=<>'. Note that because Perl is more general-purpose than awk, it doesn't do looping on input or printing by default (except with -p). ++Brandon -- Me: Brandon S. Allbery Ham: KB8JRR/AA on 2m, 220, 440, 1200 Internet: allbery@NCoast.ORG (QRT on HF until local problems fixed) America OnLine: KB8JRR // Delphi: ALLBERY AMPR: kb8jrr.AmPR.ORG [44.70.4.88] uunet!usenet.ins.cwru.edu!ncoast!allbery KB8JRR @ WA8BXN.OH
eeh@Dixie.Com (Ed Howland) (04/06/91)
allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) writes: >As quoted from <9004@rsiatl.Dixie.Com> by eeh@Dixie.Com (Ed Howland): >+--------------- >| !0 >| Is there anything shorter? Why does'nt just '1' (or any non-zero value) work? >+--------------- >It probably matches the line number, so a given positive number N would match >only on line N. (??? I have never understood all of awk completely; the >documentation for standard awk seems to leave a lot to be desired. The new >awk and GNU awk may well be different there --- new awk has a book, at least.) Several people responded that the reason was since everything in awk was treated (by default) as a string, a 1 (or other non-zero numeric) would not match the current line. By using the negate, awk interprets the next token as a number, thereby correctly evaluating the expression to true, which causes the default action (i.e. copy input to output). Interestingly, some newer implementations of awk (SYSV3) seem to think this is a syntax error (and give the famous 'bailing out...' msg.) So I haven't been able to experiment further, (need to get back to SYSV2, I guess). Makes me wonder what the shortest default prog is for these other implementations. But you may be right as well. This is helping me understand the internal workings of programs like perl and awk. >"perl -pe ''" is the shortest, but may not be fair because it uses command >line arguments to "imply" a script. >++Brandon Yes I got several answers that said -p was the shortest, I agree it might not be the same comparison. BTW does anybody know if gawk (GNU Awk) executes this? Ed. -- Ed Howland Internet: eeh@dixie.com uucp: ...{uunet,emory}!rsiatl!eeh "I am expressly forbidden to offer any kind of warranty. This covers ideas, thoughts, opinions, or just plain gibberish. They are only offered 'As is'."