0

Haw can I change a word after searching under some directory : for example change the word abc to ddd located in all .c files.

grep --include=\*.c -rnw '/path/to/somewhere/' -e "abc"

Then how can I modify these occurrences to be ddd? What is the command line?

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Feres_F
  • 13
  • 6

1 Answers1

0

I guess you're on Linux... ok?

It's very simple:

find <directory> -type f -name \*.c -print0 | xargs -0 -t sed -i 's/abc/ddd/g'

The first part of the command will find all files with extension .c under "directory" (you will have to use the real directory name here (for example a single '.' (dot) to look into your current directory). Filenames with extension .c are then passed via xargs to sed that will replace "in-place" all occurrences of "abc" with "ddd". Be aware: this command will replace ALL "abc" to "ddd" ("abc" > "ddd", "abcdefg" > "ddddefg", etc...). If you want something different or study how these commands work:

man find
man xargs
man sed
mauro
  • 5,730
  • 2
  • 26
  • 25
  • i try this one on my root directory recursivly because i have multiple poms inside there. 'grep -r --include=pom.xml -e "1.2.0-SNAPSHOT" | xargs -0 -t sed -i 's/1.2.0-SNAPSHOT/1.1.0-SNAPSHOT/g'' the out put show me all files that contains the 1.2.0-SNAPSHOT and show me message **File name too long** – Feres_F Feb 14 '16 at 13:58
  • @Feres_f Your command is wrong. Check xargs man page (option -0). It works if you have a find with -print0 in front of it... – mauro Feb 14 '16 at 17:39