Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, Can somebody help me with a shell script to do the following. It should segreagate binary files from text files in a folder and it should replace a word ...
  1. #1
    Just Joined!
    Join Date
    Jul 2003
    Posts
    17

    Script to replace word in text files

    Hi,

    Can somebody help me with a shell script to do the following.


    It should segreagate binary files from text files in a folder and it should replace a word in all the text files.

    Say for eg., it should replace NewYork with NY

    tHX

  2. #2
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    75
    To replace text in file
    sed -i 's/old_text/new_text/g file_name

    e.g.

    sed -i 's/New York/NY/g' test_file

    Hopefully this would answer half your question

    --Syd

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    To test if a file is a text file, you can use command file, for example,
    $ file test.txt
    test.txt: ASCII text

    Based on that, you can write your script like this:

    Code:
    find . -type f | while read afile; do [ -n "`file $afile|grep text`" ]  && sed -i 's/old_text/new_text/g' $afile; done

Posting Permissions

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