I am using a Fish shell and when I search through file with grep, I usually get a long output, which is difficult to further search through. If I use something like this grep -r 'email' . | less, I get an output limited to one page all right but I also loose all colors (the output is white). How do I use less while keeping colors, or is there other way to get my output one page at a time?
51645
Can I get less not to monochrome its output?  E.g., the output from git diff is colored, but git diff | less is not.
92932
Given a command that changes its behaviour when its output is going to a terminal (e.g. produce coloured output), how can that output be redirected in a pipeline while preserving the changed behaviour?  There must be a utility for that, which I am not aware of.  Some commands, like grep --color=always, have option flags to force the behaviour, but the question is how to work around programs that rely solely on testing their output file descriptor.  If it matters, my shell is bash on Linux.
77426
Is there a way to output the neighbouring lines along with the line numbers while grepping the contents of a file? I'm trying to get outputs of the form:  $ ~/myscript.sh download file # 'download' being the pattern here 121: # and downloads with wget/curl, whichever is available. 122: download_file () { 123:  if which wget &amp;&gt;/dev/null; then ---------- 356: # Since the prerequisites are already installed, we may proceed 357: download_urls=("http://example.com/foo/bar.bz2" 358:                "http://d1.example.com/xyz/abc.tar.gz"   Of course, I know that I can get grep my pattern with the -n option and show some of the neighbouring lines from the obtained line number (say, by using any of the methods ), but is there a better way to do this?
63159
I was just running a few commands in a terminal and I started wondering, does Unix/Linux take shortcuts when running piped commands?  For example, let's say I have a file with one million lines, the first 10 of which contain hello world. If you run the command grep "hello world" file | head does the first command stop as soon as it finds 10 lines, or does it continue to search the entire file first?
117087
I want to search all files in the current directory and below for the substring $form['#node']. So, I have tried, initially:  grep -R "\$form\[\\\\'" . and grep -R "\$form\[\'" .  But it returns no results. However,   grep -R "\$form\[" returns plenty of results, as there are instances of entries such as $form['#id'] or $form['#node'] in the files. Is there any reason why adding the escaped single quote causes an issue? I can't seem to find any clear indication of exactly how to escape a single quote in this case.  I need to check that my escaped characters are working as intended in this scenario.
100276
I have a command that produces output in color, and I would like to pipe it into a file with the color codes stripped out. Is there a command that works like cat except that it strips color codes? I plan to do something like this:  $ command-that-produces-colored-output | stripcolorcodes &gt; outfile
95484
I want to grep the output of my ls -l command:  -rw-r--r--   1 root root       1866 Feb 14 07:47 rahmu.file -rw-r--r--   1 rahmu user     95653 Feb 14 07:47 foo.file -rw-r--r--   1 rahmu user   1073822 Feb 14 21:01 bar.file   I want to run grep rahmu on column $3 only, so the output of my grep command should look like this:  -rw-r--r--   1 rahmu user     95653 Feb 14 07:47 foo.file -rw-r--r--   1 rahmu user   1073822 Feb 14 21:01 bar.file   What's the simplest way to do it? The answer must be portable across many Unices, preferably focusing on Linux and Solaris.  NB: I'm not looking for a way to find all the files belonging to a given user. This example was only given to make my question clearer.
92320
I am trying to print the lines using the repetition symbol {n} but it is not working.  For. e.g. I want to print all lines whose length is 4 char long   awk '/^.{4}$/' test_data   The above code is not printing that .How to fix it so that I can use the repetition symbol? I know the alternative like awk  '/^....$/' test_data and awk  'length ==3 ' test_data
111755
I have a file with hundreds of paragraphs of around 15 lines each. I need to search for a pattern, say Occurrence: 1. If this pattern is found in the para, I need to print the entire paragraph. Note that the paragraphs are separated by 2 new line characters.  I have tried the below line of code and this obviously prints the first occurrence in the file. I am somehow unable to use a loop and print all such occurrences.  sed -n '1,/Occurrence: 1/p' ystdef.txt | tail -9 &gt; ystalarm.txt   Can I use the g (global) flag with sed to make this work? If yes, how?  Note that I am aware of the grep -A/B/C commands but they wont work on my Cygwin terminal.
92353
Assuming that the grep tool should be used, I'd like to search for the text string "800x600" throughout the entire file system.  I tried:  grep -r 800x600 /   but it doesn't work.  What I believe my command should do is grep recursively through all files/folders under root for the text "800x600" and list the search results.  What am I doing wrong?
13944
I have to grep through some JSON files in which the line lengths exceed a few thousand characters. How can I limit grep to display context up to N characters to the left and right of the match? Any tool other than grep would be fine as well, so long as it available in common Linux packages.  This would be example output, for the imaginary grep switch Ф:  $ grep -r foo * hello.txt: Once upon a time a big foo came out of the woods.  $ grep -Ф 10 -r foo * hello.txt: ime a big foo came of t
22339
I'm looking for a way to list all files in a directory that contain the full set of keywords I'm seeking, anywhere in the file.  So, the keywords need not to appear on the same line.  One way to do this would be:  grep -l one $(grep -l two $(grep -l three *))   Three keywords is just an example, it could just as well be two, or four, and so on.  A second way I can think of is:  grep -l one * | xargs grep -l two | xargs grep -l three   A third method, that appeared in , would be:  find . -type f \   -exec grep -q one {} \; -a \   -exec grep -q two {} \; -a \   -exec grep -q three {} \; -a -print   But that's definitely not the direction I'm going here. I want something that requires less typing, and possibly just one call to grep, awk, perl or similar.  For example, I like how , like:  awk '/one/ &amp;&amp; /two/ &amp;&amp; /three/' *   Or, print just the file names:  awk '/one/ &amp;&amp; /two/ &amp;&amp; /three/ { print FILENAME ; nextfile }' *   But I want to find files where the keywords may be anywhere in the file, not necessarily on the same line.    Preferred solutions would be gzip friendly, for example grep has the zgrep variant that works on compressed files. Why I mention this, is that some solutions may not work well given this constraint. For example, in the awk example of printing matching files, you can't just do:  zcat * | awk '/pattern/ {print FILENAME; nextfile}'   You need to significantly change the command, to something like:  for f in *; do zcat $f | awk -v F=$f '/pattern/ { print F; nextfile }'; done   So, because of the constraint, you need to call awk many times, even though you could do it only once with uncompressed files. And certainly, it would be nicer to just do zawk '/pattern/ {print FILENAME; nextfile}' * and get the same effect, so I would prefer solutions that allow this.
32198
I have a fairly large file (35Gb), and I would like to filter this file in situ (i.e. I don't have enough disk space for another file), specifically I want to grep and ignore some patterns — is there a way to do this without using another file?  Let's say I want to filter out all the lines containing foo: for example...
74420
So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a pattern, but only to return the portion of the line after the match. The portion before and after the match will consistently vary. I have played with using sed or awk, but have not been able to figure out how to filter the line to either delete the part before the match, or just return the part after the match, either will work. This is an example of a line that I need to filter:  2011-11-07T05:37:43-08:00 &lt;0.4&gt; isi-udb5-ash4-1(id1) /boot/kernel.amd64/kernel: [gmp_info.c:1758](pid 40370="kt: gmp-drive-updat")(tid=100872) new group: &lt;15,1773&gt;: { 1:0-25,27-34,37-38, 2:0-33,35-36, 3:0-35, 4:0-9,11-14,16-32,34-38, 5:0-35, 6:0-15,17-36, 7:0-16,18-36, 8:0-14,16-32,34-36, 9:0-10,12-36, 10-11:0-35, 12:0-5,7-30,32-35, 13-19:0-35, 20:0,2-35, down: 8:15, soft_failed: 1:27, 8:15, stalled: 12:6,31, 20:1 }   The portion I need is everything after "stalled".  The background behind this is that I can find out how often something stalls:  cat messages | grep stalled | wc -l   What I need to do is find out how many times a certain node has stalled (indicated by the portion before each colon after "stalled". If I just grep for that (ie 20:) it may return lines that have soft fails, but no stalls, which doesn't help me. I need to filter only the stalled portion so I can then grep for a specific node out of those that have stalled.  For all intents and purposes, this is a freebsd system with standard GNU core utils, but I cannot install anything extra to assist.
121655
I get colored tree using tree -C but when I pipe it to less I get results like this:  tree public/ -C | less     How can I have colors in less in this case?
103962
I'm using grep in bash to search for the string "-1" .  I would usually search for "bar" like this ...  glaucon@foo $ grep -irn "bar"   ... which works fine but when I try to search for "-1" I get this ...  glaucon@foo $ grep -irn "-1" Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.   ... I thought the minus might be a special character but it appears not . I also tried using the 'fixed strings' option in case this was some aspect of regex I was unfamiliar with ...  glaucon@foo $ grep -irnF "-1" Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.   ... but, as shown, that doesn't provide output either.   I'm sure this is straightforward but ... how ?  Thanks
122315
I am using grep to recursively look through files and pull out all strings matching a pattern. I want to print the file path: matched string.   -H should print the file path for each entry  -o should print just the matched string, omitting everything else on the line I don't want  -r recursive search.   I run:  grep -Hro [pattern] .   Most of the matches print with the filename, however a few just print the matched string. Are -H and -o mutually exclusive in that -o is trimming the filename?  Sample Output:   ./path/foo.txt: foo ./path/bar.txt: foo ./path/baz.txt: foo foo ./path/baz.txt: foo   The 4th foo match should also have a file associated with it, but it is not being printed.   I am getting behavior similar to this , however I am using the -H flag, and the "/dev/null" solution does not work either.  $ grep --version grep (BSD grep) 2.5.1-FreeBSD   EDIT:  After further exploring each match where the regex was not getting the file name, they all occur when two strings are matched on the same line.  For example this text file  foo foo sometext foo foo   Outputs  ./path/foo.txt: foo ./path/foo.txt: foo foo ./path/foo.txt: foo   Anyone know a solution to get both matches to print a filename?