Hacker News new | ask | show | jobs
by uraza 2964 days ago
Grep treats the pattern as a newline-separated list of items to search for.

From https://www.gnu.org/software/grep/manual/grep.html#Introduct...: "Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text."

2 comments

Right, and actually, it looks like only 'ls | grep "${data}" ' (eg, the data var double-quoted) works:

  [jbrown@tools1 testdir1]$ data='testfile1
  > testfile2
  > testfile3
  > testfile4
  > testfile5
  > testfile6
  > testfile7
  > testfile8
  > testfile9
  > testfile10'
  
  [jbrown@tools1 testdir1]$ echo $data
  testfile1 testfile2 testfile3 testfile4 testfile5 
  testfile6 testfile7 testfile8 testfile9 testfile10
  
  [jbrown@tools1 testdir1]$ ls | grep `echo ${data}`
  <<NO OUTPUT>>

  [jbrown@tools1 testdir1]$ ls | grep ${data}
  <<NO OUTPUT>>

  [jbrown@tools1 testdir1]$ ls | grep "${data}"
  testfile1
  testfile10
  testfile2
  testfile3
  testfile4
  testfile5
  testfile6
  testfile7
  testfile8
  testfile9
Believe that using echo in the backticks may amount to same effect as 'ls | `cat ..filelist.txt` (eg, results in ENOENT)

And of course, as many have mentioned grep -f does what I was looking for and would have prevented this whole exercise:

  [jbrown@tools1 testdir1]$ ls | grep -f ../filelist.txt
  testfile1
  testfile10
  testfile2
  testfile3
  testfile4
  testfile5
  testfile6
  testfile7
  testfile8
  testfile9
For multi-line greps (rare, I guess), I use: pcregrep -Mr