Email: bknpk@hotmail.com Phone: +972-54-7649119
V

 

ASIC/FPGA Design and Verification Out Source Services

Global file search for text string replace

How to search for files and per each file do a global search of a given text string.

  1. First do a "find command":

    find -type f //all files
    find -mtime -2 //two days old
    find . -name "*.c" //all c source files

  2. Than prepare a little bash file including sed command:
  3. #!/bin/bash
    sed -e 's/timescale *.SV_TIMESCALE//' $1 > Tmp
    \mv -f Tmp $1

  4. It is possible to create a sed command with multiple replace strings:

  5. sed -e 's/erors/errors/g' -e 's/last/final/g' my_file.txt > Tmp

  6. To run it just fire up this command:
  7. find . -name "*.v" | xargs -n1 | xargs -i= ./tiRm.unx =
    where tiRm.unx is the name of the bash file.

  8. Sometimes only one file is involved. In this case vim can be used to do the conditional replace operation.
    :g/0b/s/\([01]\) \(([01]\)/\1\2/g
    This will remove space between binary [01], if it finds the binary symbol 0b
    Another example is to replace some annoying ^M, which sometimes appear, when importing files from DOS to unix.
    :g/ctrlVctrlM/s///g

  9. vim replace on blocks:
    You can use line numbers or vim marks to define a replace zone area.
    12,23g/polarity_cnt/s//polarity_cnt_dbg/g
    'a,'b g/polarity_cnt/s//polarity_cnt_dbg/g
    where the block is defined by the vim commands ma and mb.
    To run a script on a block from vim go to : perl script on run on block form within vim

  10. Or do it from the command line using perl to do the job of string replace:
    perl -i -pe 's/00/99/;' Italian_top_100.txt

  11. The following script may be helpful, should you need to replace a string in many files:

  12. #!/bin/bash

    sed -e 's/replcae me/with this/' $1 > Tmp
    #remove empty lines
    #sed '/^ *$/d' $1 > Tmp
    \mv -f Tmp $1

    Note the the script is extremely simple. But its operation is a bit complex:
    find . -name "*.c" | xargs -n1 | xargs -i= replace_with_sed_script.unx =
  13. A similar issue is discussed at: search into multiple files and generate a report The solution in this page uses a for f (similar to perl's foreach statement) and therefore results in a simpler script.

Contact me now at:

  ...


I would be happy to offer my services. Call ASAP !


Home

A very simple perl script, to be run from within vim, to put an incremental value in binary format.






Search This Site


Feedback This Site




new pages on this site