Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's fun to write your own tools! (Scratch your own itch and all that.)

It sounds like your "line" replicates a use of sed that I use all the time, printing a contiguous range of lines.

The example:

line file.txt 5 to 9

can be:

sed -n '5,9p' file.txt

You mention using awk, which totally works, but to me is much less ergonomic.

You don't explain what line's "--column" option does so I'm not sure what the equivalent of that might be. That might be where awk comes into its own ... :)

> [...] and printing lines between matching words

This is the same sed command as above, but using regular expressions for the address part:

sed -n '/^func doit/,/^}$/'

will print just the function called "doit" (in properly formatted go).

Could you (I mean "one") design a "friendlier" (or more "beginner friendly") user interface than sed presents? Yes, obviously (you did exactly that). But unlocking the power (or even just beginning to "unlock" the power) of the standard tools (sed, awk, grep, tr, cut, paste, find, xargs, ...) can get you a really long way. Of course, the initial problem is how to know that one of those tools can solve the problem you have in your head.

("Bonus" sed content: replace "head":

Instead of

head -n 5

do

sed 5q

To replace tail you need tac (or "tail -r", haha))



> You don't explain what line's "--column" option does

It was simply the equivalent of Awk printing columns like this.

awk '{ print $2, $3, $4 }'

Except with support for column ranges, which I don't think Awk supports without writing a for loop.

Something convoluted like :

awk -v start=1 -v end=3 '{ for (i=start; i<=end;i++) printf("%s%s", $i,(i==end) ? "\n" : OFS) }' marks.txt

Idk, the amount of Unix that GNU and BSD accepted as a bare environment, the range is single purpose programs like tail and mini interpreters which are powerful but require a lot of skill. If I worked in system administration, and I HAD to use shell, I'd hold onto Awk for dear life. But I don't, so it's sort of this ancient swiss army knife.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: