1
grep -Eri "drucken" app/views

app/views/meths/_form.html.erb:                                <li class="hidden-phone"><a onclick="javascript: print();" class="">Methode drucken</a></li>
app/views/clients/show.html.erb:                    <li><a onclick="javascript: print();" class="" value="drucken">drucken</a></li>
app/views/clients/index.mobile.html.erb:                                <li class="hidden-xs"><a onclick="javascript: print();" class="">KlientInnen drucken</a></li>
app/views/treatments/index.html.erb:                        <li><a onclick="javascript: print();" class="" value="drucken">drucken</a></li>

I'm looking for a solution where I could do something like:

grep -Eri "drucken" app/views | xargs INTO_A_FANCY_TOOL_WHICH_REMOVES_THOSES LINES

update:

I want to remove those lines from the file.

  • sed should do this for you. – mkc Feb 28 '15 at 15:57
1

Did you try something like:

grep -Eri -l "drucken" app/views | xargs sed -e '/drucken/d' -i

where '-l' tells grep to only print the file name, '-i' tells sed to modify that file on the fly.

Alternatively you could loop with sed over all files, but it will "touch" all of them even if the file doesn't contain the requested word:

find app/views -type f -exec sed -e '/drucken/d' -i {} \;
  • As other users said, you can of course use grep -v, but you'd need a loop like (assuming you have all files in the same views folder, use a find otherwise): for f in app/views/*; do grep -v "drucken" $f > $f.new ; mv $f.new $f; done – Ariel Feb 28 '15 at 16:14
  • Do it by ONE prog sed -ie '/drucken/d' app/views/*/*.html.erb – Costas Feb 28 '15 at 16:14
  • @Costas sed -i <list of files> will "touch" all your files even if they do not contain the unwanted term (so will do my other proposal above). If that is not important, then yes a single sed should do. – Ariel Feb 28 '15 at 16:18
  • @Costas sed will not recursively search files in a folder tree, so all the files to modify must be therefore listed in the command line. – Ariel Feb 28 '15 at 16:26
1

Looks to me like you're looking for grep -v?

  -v, --invert-match
         Invert the sense of matching, to select non-matching lines.  (-v
         is specified by POSIX.)

So grep -v 'drucken' file1 > file2 will give you a file2 with those lines removed.

Alternatively and untried, something along the lines of

sed -e '/drucken/d' infile > outfile
  • But I'd like to remove those lines from the file. – AME Feb 28 '15 at 15:58
  • @auL5agoi: That's what grep -v does, apart from not having a n "in place" switch. – Ulrich Schwarz Feb 28 '15 at 16:02
1

You could use a perl oneliner for that:

$ cat file
a
b
c
$ perl -i -wne 'if(/b/){print STDOUT $_}else{print}' file
b
$ cat file
a
c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.