3 ms·
Mastering one tool is probably wise, but it's also good to know which tools may be better suited for some purposes. For "keep the first word of every line", I
by blunte 5y ago
Mastering one tool is probably wise, but it's also good to know which tools may be better suited for some purposes.
For "keep the first word of every line", I prefer awk:
awk '{ print $1 }' < file.txt
- brabel 5y agoI agree with the people here saying that it's better to just use your favourite scripting language unless your job is mostly about writing little bash scripts like that (maybe sysops people?)... Here's a groovy script to do the same: new File('file.txt').eachLine { println it.split()[0] } Not as neat as awk, but not much worse either... and as I use Groovy a lot for testing, and its syntax is simplified Java (which I use a lot as well) it's just much better for me... if I had to ssh into other servers where Groovy or Lisp (my other favourite "scripting" language) are not available, then yeah, I would learn awk better (I actually like awk, and use it sometimes for the rare occasion I don't have groovy/lisp installed).
- blunte 5y agoIn cases where it's a one time use, anything that gets you an answer easily is great. Could be Groovy, Ruby, Python, Perl, etc. or of course these bin tools. But if it's going to be used a lot, and start-up time matters, then some of the full programming languages can become expensive for the temporary load they create just spinning up. In those cases, the standard Unix tools tend to be so much faster and lighter that it's worth learning them.
- anthk 5y agoPerl is much better than Pyhon for that for obvious reasons, Perl is like an improved sed with stereoids with some C inspiration.
- dolmen 5y agoUse the best tool for the job. sed is just a DSL (domain specific language) specifically designed for for manipulating text files line by line. Note that for matching strings even in Groovy/AWK/Python/Perl/... you'll probably use regexp which are another DSL. Would you recommend to use string functions instead?
- brabel 5y ago> Would you recommend to use string functions instead? Actually, yes, of course. Regex should be the last resort, really. It's unreadable, presents security issues depending on where it's used, and much of the time using string functions in a "proper" language will be actually easier unless you're a regex expert (which is a pretty silly thing to acquire expertise on).
- kerblang 5y agoI adore Groovy the language but am compelled to note its horribly slow bootup time. On a commodity laptop it's usually a couple of seconds - acceptable for some things, but hard to justify for one-liners.
- tyingq 5y ago>For "keep the first word of every line", I prefer awk: Me too, though the awk version throws out any leading whitespace, so it's not quite the same.