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 ...
- 12-19-2008 #1Just 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!
- 12-20-2008 #2Linux 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.
- 12-21-2008 #3
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:
This replaces every occurrence of "ABC" with "TTT".Code:sed -e 's/ABC/TTT/g'
Good luck!DISTRO=Arch
Registered Linux User #388732


Reply With Quote