Earlier quoted context omitted.
Here's the equivalent for Ruby: alias glorp='ruby -ane ' $ [data is generated] | glorp ' ~ /Something/ and puts $F[2]' Or: $ echo -e '{"hello":"world"}\n{"hello":"cat"}' | glorp 'puts JSON.load($_)["hello"] ' -rjson (Of course Ruby got the -a autosplit-mode and the -n assumed 'while gets(); ... end' loop from Perl along with $_ and $F, so it's very intentional that they're similar)
And Ruby regexes are amazing.
Skip grep, use awk
121–130 of 136 posts
Re: Skip grep, use awk
#122Re: Skip grep, use awk
#123Earlier quoted context omitted.
I wish the default 'cut' implementation could be just a little more clever - regex delimiters would be good, it doesn't even support multiple characters :( Also, cut's output manipulation is surprising. '-f 2,1' is actually the same as '-f 1,2' - you can't change the order of printing. I know there are other programs that can do the job, but it's a little frustrating when you can 'almost' get there with a chain of pi…
If you pipe your file through "while read f1 f2 f3 ; do echo field2 is $f2 ; done" for example you can pick out fields. Re-ordering them is just of a special case of any sort of bash manipulation you can do in the loop body. Admittedly for the very basic case, it's not as terse as "cut -f2" but if you're doing any further processing on the stream then I find it's often shorter.
Re: Skip grep, use awk
#124Re: Skip grep, use awk
#125Yay, now I've found a replacement for grep under macOS. Thanks for that!
Re: Skip grep, use awk
#126Earlier quoted context omitted.
why do you need `while read` part at all? this is the main slow down here. shell is extremely time consuming compared to sed/awk/whatever tool.
The script is currently a filter - the input comes from stdin.
cat
or, cat myfile
A Brave Cow
Was Walking
Crows Were Airborne
All Was Well
EOF
sed -e 's/A/X/;s/B/Y/;s/C/Z/;'
or (copy-pasted from shell for clarity), $ cat foo.sh
#!/bin/sh
sed -e 's/A/X/;s/B/Y/;s/C/Z/;'
$ cat Re: Skip grep, use awk
#127Skip awk, use perl.... The alias below sets perl to loop over STDIN splitting each line on more than one whitespace character and populate an array F. The -nE will then Evaluate an expression from the command line looping over the input line-by-line. alias glorp='perl -aF"/\s+/" -nE' So now we have the command `glorp` to play with which has more familiar syntax than awk and all of CPAN available to play with! $ [data…
I can think of two very strong reasons to prefer awk over perl:
* busybox implements awk, so its (usually) available even on embedded platforms
* awk syntax is way clearer than perl, anyone that has a few experience in C + shell can figure out what is being done with some googling
awk is also powerful enough to separate output to different files, accumulate inputs, etc. And if you need anything more complex, there are tons of other languages to choose from then (including perl).
Re: Skip grep, use awk
#128Earlier quoted context omitted.
The script is currently a filter - the input comes from stdin.
You can redirect stdin to sed directly, e.g., cat or, cat myfile A Brave Cow Was Walking Crows Were Airborne All Was Well EOF sed -e 's/A/X/;s/B/Y/;s/C/Z/;' or (copy-pasted from shell for clarity), $ cat foo.sh #!/bin/sh sed -e 's/A/X/;s/B/Y/;s/C/Z/;' $ cat
sed -e ''
into the script, but the solution is now much simpler and faster.Re: Skip grep, use awk
#129Skip awk, use perl.... The alias below sets perl to loop over STDIN splitting each line on more than one whitespace character and populate an array F. The -nE will then Evaluate an expression from the command line looping over the input line-by-line. alias glorp='perl -aF"/\s+/" -nE' So now we have the command `glorp` to play with which has more familiar syntax than awk and all of CPAN available to play with! $ [data…
what's the reason of using -F option? -a defaults " " separator, which already emulates awk behavior. If I'm not wrong, only difference between perl -aF"/\s+/" and perl -a is treating of leading whitespace in lines
Re: Skip grep, use awk
#130I'm sure the author is aware, but awk has at least three implementations: nawk (the one true), gawk (what most are using), and mawk (performance-oriented, unmaintained). Plus busybox-awk. When benchmarking gawk, I've found using LANG=C and avoiding UTF-8 to make a substantial difference for pattern matching.