LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-19-2008, 08:18 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Editing contents of 8 files at once


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


This seems not to work.
Please assist!
 
Old 12-19-2008, 10:28 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 12-19-2008, 11:53 AM   #3
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
'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
 
Old 12-19-2008, 12:04 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Why the loop?


sed -i 's/ABC/TTT/g' /temp_folder/*
 
Old 12-19-2008, 12:17 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 12-19-2008, 02:00 PM   #6
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Quote:
Originally Posted by Tinkster View Post
Why the loop?


sed -i 's/ABC/TTT/g' /temp_folder/*
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.
 
Old 12-19-2008, 02:03 PM   #7
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Quote:
Originally Posted by colucix View Post
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.
 
Old 12-22-2008, 04:07 AM   #8
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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 :-).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
text files contents jaepi Programming 5 06-18-2008 10:07 AM
Bash script for editing contents of one file according to other kkpal Linux - Newbie 8 02-26-2008 03:49 AM
Comparing files contents? hhegab Linux - Newbie 3 07-01-2004 12:45 AM
to compare the contents of 2 files MaleWithBrains Linux - Newbie 3 01-27-2004 06:39 PM
Deleting contents of files. Road Linux - General 2 10-03-2002 06:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration