Grep with multiple parameters

Asked

Viewed 3,476 times

2

I need to select the lines of a file that contains the characters | or \

diff -y ontem.csv hoje.csv | grep -e "|" -e "\"

How to tell pro grep to return the lines it contains or a pipe | or a bar \ ??

  • What returns in this command you posted ?

  • Returns the lines of the diff, as if the grep had not been done.

3 answers

5

You can use regular expression [\|], look at you:

grep:

$ diff -y ontem.csv hoje.csv | grep '[\|]'

egrep:

$ diff -y ontem.csv hoje.csv | egrep '[\|]'

awk:

$ diff -y ontem.csv hoje.csv | awk '/[\\|]/{print}'

perl:

$ diff -y ontem.csv hoje.csv | perl -nle 'print if m{[\\|]}'

sed:

$ diff -y novas.csv entrada.txt | sed -n '/[\|]/p'

3


You can use the delimiters [], that take all the characters inside them (to \ must be escaped, so stay \\):

grep -e "[\\|]" 

See examples of this regex here.


As reported in the comments, the grep also works without the option -e and no need to escape the bar:

grep "[\|]" 
  • The parameter -e is dispensable and there is no need to escape the bar.

  • @Lacobus is right. Is that first I tested in regex101.com and there only works if escaping the bar, but in grep does not even need. As for the parameter -e, It’s an old habit I always have to use, even when you don’t need it...

3

  • No need to escape the bar or pipe.

  • @Lacobus Valew for correction! Abs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.