When I run grep like this, I can see the matches as expected:  $&gt; echo -ne 'foo\nfoo\n' &gt; file_a $&gt; grep -Hn foo file_a file_a:1:foo file_a:2:foo   But when I create a file with carriage returns in it, grep gives me unexpected output:  $&gt; echo -ne '\x0dfoo\x0dfoo\nfoo\n' &gt; file_b $&gt; grep -Hn foo file_b fooe_b:1: file_b:2:foo   Can anyone explain why it gives this output?
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?
7069
Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash?  For example, given the file:  foo bar \ bash \ baz dude \ happy   I would like to get this output:  foo bar bash baz dude happy
98896
I'm trying to pipe the output of a grep search into the input of another grep. Such as:  grep search_query * | grep -v but_not_this   But the second grep is not using the output of the previous search. It looks like the second grep is just using * instead. For example,  grep lcov *                                                                                           tst/bits/Module.mk:21:$(call func_report_lcov) tst/drivers/Module.mk:27:$(call func_report_lcov)   But when I want to filter out the results containing "call",  grep lcov * | grep -v call ...   Grep gives me every single line in my workspace that doesn't contain "call".  Environment Info:   This is happening in both bash and fish I have aliased the grep command like so alias grep='grep -nR --color=always'   Anything else I might be missing?
70438
I want to be able to capture the exact output of a command substitution, including the trailing new line characters.  I realise that they are stripped by default, so some manipulation may be required to keep them, and I want to keep the original exit code.  For example, given a command with a variable number of trailing newlines and exit code:  f(){ for i in $(seq "$((RANDOM % 3))"); do echo; done; return $((RANDOM % 256));} export -f f   I want to run something like:  exact_output f   And have the output be:  Output: $'\n\n' Exit: 5   I'm interested in both bash and POSIX sh.
44404
While we use * to denote zero or more previous characters in grep, we use *.c to find all C files when we use it with the ls command like ls *.c. Could someone tell how the use of * differs in these two cases?
100821
It seems I am misusing grep/egrep.   I was trying to search for strings in multiple line and could not find a match while I know that what I'm looking for should match. Originally I thought that my regexes were wrong but I eventually read that these tools operate per line (also my regexes were so trivial it could not be the issue).  So which tool would one use to search patterns across multiple lines?
31032
I'm paranoid about this, but hopefully it's a simple question:  Is there any difference between  cat file1 | egrep -oP "[Mm]y string" &gt; /home/user/file.txt   and  cat file1|egrep -oP "[Mm]y string"&gt;/home/user/file.txt   i.e., does it matter if I have spaces padding the pipe (|) and redirection (>) characters? I'm really paranoid that having spaces there the spaces will somehow end up in the output file when I don't want that. Can anyone clarify? (Using Ubuntu 12/bash if that matters.)
48554
I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example:  $ cat &gt; alpha.txt This is a test Please do not be alarmed  $ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt  $ diff alpha.txt{,.original}  $ # No differences printed out   How can I get this to work?
104938
Trying to use grep today, I ran into the  of the  in a Unicode file (UTF-8, in this case). Specifically, I was trying to find a file beginning with XYZ with the pattern grep '^XYZ', but of course grep treated the BOM as three separate characters and did not match the first line of the file if the first line started with XYZ. I even tried to update the regular expression to ignore spaces ('^[[:space:]]*XYZ'), but to no avail.  Other questions have dealt with  or , but I want to know if POSIX tools have a general option to handle Unicode files correctly. If grep handled the Unicode file correctly, it would consider the file contents to start after the BOM and match XYZ on the first line just like any other line.
18187
Can somebody explain what is going on in this command:  echo foo &gt;&amp;2 | grep foo   Zsh (5.0.2) prints foo twice. One seemingly to stdout and one to stderr, since running these commands:  { echo foo &gt;&amp;2 | grep foo } &gt;/dev/null   and:  { echo foo &gt;&amp;2 | grep foo } 2&gt;/dev/null   both just print foo once.
113284
I have a file called -.  I want to display its contents.  One way is to do  cat ./-   since cat - reads from standard input. However, why are cat "-" and cat '-' also interpreted by the shell as cat -?
40404
Very new to UNIX but not new to programming. Using Terminal on MacBook. For the purposes of managing and searching word lists for crossword construction, I'm trying to get handy with the Grep command and its variations. Seems pretty straightforward but getting hung up early on with what I thought should be a simple case.  When I enter   grep "^COW" masternospaces.txt   I get what I want: a list of all the words starting with COW.  But when I enter   grep "COW$" masternospaces.txt   I expect to get a list of words ending with COW (there are many such words), and nothing is returned at all.   The file is a plain text file, with every line just a word (or a word phrase with no spaces) in all caps.  Any idea what could be happening here?
34780
I want to know which files have the string $Id$.  grep \$Id\$  my_dir/mylist_of_files   returns 0 occurrences.  I discovered that I have to use  grep \$Id$ my_dir/mylist_of_files  Then I see that the $Id is colored in the output, i.e. it has been matched.  How could I match the second $ and why doesn't \$Id\$ work.  It doesn't matter if the second $ is the last character or not.  I use grep 2.9.    Before posting my question, I used google...       To search for a $ (dollar sign) in the file named test2, enter:      grep \\$ test2      The \\ (double backslash) characters are necessary in order   to force the shell to pass a \$ (single backslash, dollar sign) to the   grep command. The \ (single backslash) character tells the grep   command to treat the following character (in this example the $) as a   literal character rather than an expression character. Use the fgrep   command to avoid the necessity of using escape characters such as the   backslash.   but I don't understand why grep \$Id works and why grep \\$Id\\$ doesn't.  I'm a little bit confused...
56238
Bash 4.2 on CentOS 6.5:  In my ~/.bash_profile I have a bunch of aliases, including:  alias grep='grep -n --color=always'   so that I can get color highlighting and print line numbers automatically when running grep. If I run the following, highlighting works as expected:  $ grep -Re 'regex_here' *.py   However, when I ran this recently:  $ find . -name '*.py' | xargs grep -E 'regex_here'   the results were not highlighted and line numbers weren't printed, forcing me to go back and explicitly add -n --color=always to the grep command.   Does xargs not read aliases in the environment? If not, is there a way to make it do that?
94270
The command that I have prints out two of the lines whenever FW_6.0.0 is found, below is the code:  grep -oP 'FW_6.0.0, (.*)$' file   Below is the output, both has the same value of FW_6.0.0  FW_6.0.0, SUCCESS FW_6.0.0, OK   I would like to match two words, that is FW_6.0.0 and SUCCESS that can be found on the same line so that it prints this out:  FW_6.0.0, SUCCESS and eliminates FW_6.0.0, OK
92786
Why do brackets in a grep pattern remove the grep process from ps results?  $ ps -ef | grep XXXX  [...] XXXX [...] grep XXXX   $ ps -ef | grep [X]XXX  [...] XXXX
93093
I have a file with this content: $ cat compromised_header.txt some unique string 1 some other unique string 2 another unique string 3  I wanted to find all files that have all the lines of above file exactly in the same order and those lines have no intermediary lines in between. Example input file: $ cat a-compromised-file.txt some unique string 1 some other unique string 2 another unique string 3 unrelated line x unrelated line y unrelated line z  I tried using below grep: grep -rlf compromised_header.txt dir/  But I wasn't sure it will give the expected files as it will also match this file: some unique string 1 unrelated line x unrelated line y unrelated line z