Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    find and replace "word" in files | command please

    hello guys i've an issue here i hope someone help me out
    i've main dir call VB inside this i've php files and subfolders also has php files i need to replace a line in all these php fils with another line anyone can help me out with command or right way to do so,

  2. #2
    Just Joined! cold_candor's Avatar
    Join Date
    Jul 2006
    Phoenix, AZ
    Good place to start: man grep

    That's the program I usually see related to finding things in files. I know there's another program you can pipe the results to to perform the replace, but I can't think of it's name (maybe sed?).

    Hope this helps!

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    Correct. grep will find matching lines, sed will change lines. sed relies on regular expressions, so you'll need to know a little bit about those to use it to its fullest power.

    Type "man sed" for some more information, and feel free to ask questions!

  4. $spacer_open
  5. #4
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Also, stupid amounts of tremendously useful sed info and tips here.


  6. #5
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Hi, xMOe.

    I think you have two sub-goals here. Firstly you need to look at something such as sed for doing the actual substitution. Secondly, you apparently have a hierarchy through which you must navigate. That means you need to be sure that your program or script can find all the files you care about. For example, grep was mentioned. Among its capabilities are being able to descend into directories and search there. However, grep does not do substitution. The sed command does do substitution, but it does not descend into directories by itself. You can, however, provide sed with a pathname, say consisting of one or more directories followed by a filename, such as d1/d2/d3/f1. Virtually all *nix commands deal with these constructs correctly. The trick is to find all those files in which you are interested.

    I looked over the sed link that DThor posted. I found the descriptions to be somewhat terse, so one needs to look at almost every script to see if it addresses one's problem. The documentation links may be more useful. I found only one link off that page that seemed to be bad (or at least took longer than I had patience for).

    So, as mentioned, you probably should look at sed (other possibilities include python, perl, and even the interactive editor, ed, if you can tolerate the risk of modifying a file in-place). If you choose sed, you need to be aware that it is a stream editor, and, unless you specify otherwise, it will write to the terminal (so-called "standard output", stdout) -- it will not re-write the input file. You should pay special attention to the "-i" option. Think of sed as a looping machine -- get the next line, modify the line, write the line out.

    If you have only a few directories, you might decide to simply process the contents manually -- cd to the directory and run your command. Otherwise, you'll probably need to look at something like command find, which displays pathnames of files, and can optionally run a command itself. It is not a trivial command to learn, however.

    Summary: 1) get a substitution command running, 2) visit manually or automatically the directories and run your command.

    Once you have a done a few experiments, let us know if you have any specific questions.

    You'll learn a lot from this; best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    I agree the sed link has a lot of terse info - however it has something even better than the man page - links to tons and tons of practical, end use examples. I've found it's usually faster to massacre an existing example rather than going to the considerable trouble of actually learning all the subtleties of sed.

    For me, scanning those is faster than engaging my left brain.

    However, valid points.


  8. #7

    Try gawk Also

    Hi xMOe,
    Like others have mentioned learn sed. Another great utility for text manipulation is gawk. Read manual page of gawk and of course, do a web search to learn more.

    With Regards,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts