Live data from Hacker News

look(1): lines beginning with given string

linux.die.net

21–30 of 47 posts

Re: look(1): lines beginning with given string

#21
post #15

Earlier quoted context omitted.

When sorting the file first I get no output anymore: sort ~/.zshrc -o /tmp/.zshrc look "#" /tmp/.zshrc Sorry for the noise but I am confused... :|

sort(1) might not use the same ordering as look(1) Try setting LC_COLLATE=C and export it and retry.

Uh, wow! That was it!

Thanks for this answer...

Re: look(1): lines beginning with given string

#22
post #2

The universe of Unix filters never ceases to turn up things I haven't seen before. Other examples for those inclined: tac, sponge, pv, join, paste.

In the same vein of 'improvements over things everyone assumes you have to do with the more famous tool', we could say grep '^string' : look :: sort -R : shuf

Re: look(1): lines beginning with given string

#24
post #15
post #12

Earlier quoted context omitted.

From the manpage: > As look performs a binary search, the lines in file must be sorted... I assume you didn't sort your source file first.

When sorting the file first I get no output anymore: sort ~/.zshrc -o /tmp/.zshrc look "#" /tmp/.zshrc Sorry for the noise but I am confused... :|

It doesn't make sense to sort a configuration file, order and context matters. Easier to just use grep '^#' (or even better, ag).

Re: look(1): lines beginning with given string

#25
post #15

Earlier quoted context omitted.

When sorting the file first I get no output anymore: sort ~/.zshrc -o /tmp/.zshrc look "#" /tmp/.zshrc Sorry for the noise but I am confused... :|

It doesn't make sense to sort a configuration file, order and context matters. Easier to just use grep '^#' (or even better, ag).

Yes, I am aware of that but I wanted to understand the usage of 'look' at this point. It was a fault to try stuff using the .zshrc file at first because I ran into misunderstanding everything. It was just the first file that came in mind to me. Since sorting a source file and then "looking" for comments makes no sense in any way, grep seems to be really the better choice.

Re: look(1): lines beginning with given string

#27
post #23

If you don't have this basic linux utility, you can use grep '^string' file.txt but there's a greater chance of having look installed than grep.

As long as I have been using *nixes, I have never met a machine that didn't have grep. It was written by Ken Thompson himself in 1974, or 41 years ago, and first came out in Unix 4th edition.

Re: look(1): lines beginning with given string

#29
post #22
post #2

The universe of Unix filters never ceases to turn up things I haven't seen before. Other examples for those inclined: tac, sponge, pv, join, paste.

In the same vein of 'improvements over things everyone assumes you have to do with the more famous tool', we could say grep '^string' : look :: sort -R : shuf

Thanks to @climagic on Twitter, I recently learned that sort -R and shuf are not in fact the same thing. (sort -R doesn't actually shuffle...)

Re: look(1): lines beginning with given string

#30

Earlier quoted context omitted.

I created a 1.1G file with dictionary words and timed... % du -h a 1.1G a % time look 'dog' a | wc -l 53856 real 0m0.021s user 0m0.020s sys 0m0.003s % time grep '^dog' a | wc -l 53856 real 0m28.593s user 0m0.977s sys 0m2.223s Ok grep performed worse than what I expected (sort took a long time though).

Is grep optimized to be fast, though? There seems to be a lot of, "better at grep than grep" tools out there, http://beyondgrep.com/

Yeah there's a lot of that, but grep is like using duct tape to hold something together when you really should be using screws or so. grep can do everything, but its not always the best tool for the job.
Post reply on HN