4

This is my bash command

grep -rl "System.out.print" Project1/ | 
    xargs -I{} grep -H -n "System.out.print" {} |
    cut -f-2 -d: |
    sed "s/\(.*\):\(.*\)/filename is \1 and line number is \2/

What I'm trying to do here is,I'm trying to iterate through sub folders and check what files contains "System.out.print" (using grep) using 2nd grep trying to get file names and line numbers using sed command I display those to console. from here I want to remove "System.out.print" with "XXXXX" how I can pipe sed command to this? pls help me thanxx

123Ex
  • 237
  • 2
  • 5
  • 9
  • why don't you simply use `grep -rn "System.out.print" Project1/` instead of that monster pipe? –  Dec 23 '10 at 10:14
  • 1
    the thing is I want to replace "System.out.print" with "XXXXX",Can you tell me better way to do this,I'm bit new to this – 123Ex Dec 23 '10 at 10:17
  • 1
    repeating yourself verbatim does not make your question clearer –  Dec 23 '10 at 10:22
  • let me tell this way,there lot of files(directory structure) contains "word1" I want to replace that word with "word2" so could you tell me how I can do this? – 123Ex Dec 23 '10 at 10:35
  • 1. i already did tell you in my answer. 2. what does that have to do with the script you pasted? –  Dec 23 '10 at 12:41

2 Answers2

11

GNU sed has an option to change files in place:

find Project1/ -type f | xargs sed -i 's/System\.out\.print/XXXXX/g'

Btw, your script could be written as:

grep -rsn 'root' /etc/ |
    awk -F: '{ print "filename is", $1, "and line number is", $2 }'
  • or maybe i have completely misunderstood your question… could you please try to give a clearer description of what you want? –  Dec 23 '10 at 10:16
  • Is this working in recursive way??I want to access sub folders also – 123Ex Dec 23 '10 at 10:19
  • Actually this is not my home work I have project with lot of S.O.P so I want to remove those.I tried various ways,but I couldn't find a way to do this.I want to know how I can pipe grep with sed,I'm asking a bit help from you guys – 123Ex Dec 23 '10 at 10:32
  • 1
    @hop I'd go with find's `-exec` since only one command is needed (sed) to be run with the filename as an argument. – plundra Dec 23 '10 at 10:44
  • `find` is recursive unless you specify additional options (see `man find`). – l0b0 Dec 23 '10 at 12:35
  • @plundra: sure, but i wanted to avoid introducing too many new concepts. –  Dec 23 '10 at 12:39
1

I'm just building on hop's answer, which I found to be more useful than find -exec. I had search_text dispersed all over my computer, in logs, config files and so on, but I didn't want to search (or especially change) anything in /dev, /sys, /proc, and so on. One note, read man xargs; it doesn't like file names with spaces.

grep -HriIl --exclude-dir=dev --exclude-dir=proc --exclude-dir=sys search_text / | xargs sed -i 's/search_text/replace_text/g'
keyser
  • 18,829
  • 16
  • 59
  • 101
John Q
  • 11
  • 1
  • `grep | xargs` is nice too. `find -print0` | xargx -0` is a good way to handle spaces in filenames. –  Sep 22 '16 at 12:24