Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
What does not work exactly? Do you receive some error or just don't get the expected result? Two things I can notice, here. First, the script does not actually edit the files: it simply stores the file content in a shell variable, which is overwritten at each loop. Second, the tr command is not suitable for this kind of substitution, since "ABC" is not interpreted as a string of three characters but as a set of characters, in the sense that every occurrence of A or B or C is substituted by the characters in the second set, not only the literal ABC string.
I suggest to look at the sed editor which gives the possibility to substitute a whole pattern and to edit the files in place, using the -i option. This is a very good tutorial on sed. Anyway, give it a try and ask again if you encounter any problem.
'tr "ABC" "TTT" < input-file > different-output-file && mv different-output-file input-file' translates all occurrences of "A" to "T", all occurrences of "B" to "T", and all occurrences of "C" to "T".
'tr "ABC" "CBA" < input-file > different-output-file && mv different-output-file input-file' translates all occurrences of "A" to "C", all occurrences of "B" to "B", and all occurrences of "C" to "A".
'sed -i "s/ABC/TTT/g" input-file' will change all occurrences of "ABC" to "TTT" in input-file.
A for loop will operate on all your files in turn:
for file in temp_files; do
sed -i 's/ABC/TTT/g' $file
done
I'd prefer to not give complete solutions to homework questions, just give some hints and let the student move his brain. Maybe it was not so clear it is homework, anyway.
Good point... I tend to use them when I know I'll be doing more with a file than a single operation like that, which more often than not, I will be. Habit, I guess.
I'd prefer to not give complete solutions to homework questions, just give some hints and let the student move his brain. Maybe it was not so clear it is homework, anyway.
And if it is, he has only himself to blame if he has no clue later how to cope with editing large numbers of files. Oh, well.
Thanks guys! You guys are great! All the tips worked perfectly fine although I struggled because I confused ' with ` and it gave me errors but I managed to come around it.
And a big thanks to colucix for the tutorial, it will keep me reading:-)
And for all of you guys I dont mean you give me tutorials as solutions please, but they can always make a good company with the solutions I believe :-).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.