Find the answer to your Linux question:
Results 1 to 3 of 3
Hi please help me with the script of going into a directory and edit each file in there. e.g. in /temp there are 8 files and each contain content that ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    2

    Changing file contents

    Hi
    please help me with the script of going into a directory and edit each file in there. e.g. in /temp there are 8 files and each contain content that contains ABC as consecutive characters somewhere. so, the challenge is to change the ABC to TTT each time it occurs in all the 8 files. the following is what i attampted:

    cd /home/temp
    for file in temp_files
    do
    file2=`cat $file | tr "ABC" "TTT"`
    done



    Please assist!

  2. #2
    Linux User
    Join Date
    Mar 2008
    Posts
    287
    Try using grep -n ABC /tmp/*
    to print line numbers of line in every /tmp file containing ABC
    Use those line numbers & file names with awk to change the value to TTT.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Seems like you have a good start. The problem with your above script is that you used 'tr'. tr maps a character from the first set to its corresponding character in the second. So in the above, you are mapping 'A' to 'T', 'B' to 'T', and 'C' to 'T'. What are you are not doing is mapping 'ABC' to 'TTT'. Every 'A', 'B', and 'C' will be translated.

    What you are looking for is a search-and-replace, which is something that sed can do very easily. For instance, for the above:
    Code:
    sed -e 's/ABC/TTT/g'
    This replaces every occurrence of "ABC" with "TTT".

    Good luck!
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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