LinuxQuestions.org
Help answer threads with 0 replies.
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 05-07-2007, 05:17 AM   #1
pompadir
LQ Newbie
 
Registered: Apr 2007
Posts: 7

Rep: Reputation: 0
i need help with a little script...


hi,

I need to change a specific word to another in several text files which located in one directory.

for example: change all the "AAAA" to
"BBBB" in the files that have ".txt"
extension in the /log directory.

I know that's easy, but I don't know how to do it...

thanks.
 
Old 05-07-2007, 05:30 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Sed makes that a snap:
Code:
cd log
sed -i 's/AAAA/BBBB/g' log/*.txt
The -i flag causes sed to make the edits in-place, otherwise you would need to use a loop and redirect the output to a temporary file.

Last edited by jschiwal; 05-07-2007 at 05:31 AM.
 
Old 05-07-2007, 05:33 AM   #3
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by pompadir
hi,

I need to change a specific word to another in several text files which located in one directory.

for example: change all the "AAAA" to
"BBBB" in the files that have ".txt"
extension in the /log directory.

I know that's easy, but I don't know how to do it...

thanks.
Code:
for LIST in `ls *.txt`; do cp $LIST $LIST.bak; cat $LIST| sed 's/AAAA/BBBB/g'>$LIST; done
Basically for each file that can be seen using ls *.txt command
to test use
Code:
ls *.txt
in a konsole

back each up file up by adding .bak to a copy.

load the text file with cat and pass it through sed to replace AAAA with BBBB
then dump it to the same file name variable $LIST.

to test the text replace in a konsole use
Code:
cat filename.txt| sed 's/AAAA/BBBB/g'
 
Old 05-07-2007, 07:30 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by carl0ski
Code:
for LIST in `ls *.txt`; do cp $LIST $LIST.bak; cat $LIST| sed 's/AAAA/BBBB/g'>$LIST; done
I don't know why you want to run ls:
for LIST in *.txt; do cp $LIST $LIST.bak; cat $LIST| sed 's/AAAA/BBBB/g'>$LIST; done
 
Old 05-07-2007, 05:15 PM   #5
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by jschiwal
I don't know why you want to run ls:
for LIST in *.txt; do cp $LIST $LIST.bak; cat $LIST| sed 's/AAAA/BBBB/g'>$LIST; done
old school force of habit
 
Old 05-07-2007, 05:45 PM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
UUOC - no need to use cat either:
Code:
cat $LIST| sed 's/AAAA/BBBB/g'>$LIST
can be:
Code:
sed 's/AAAA/BBBB/g' $LIST > $LIST
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
How to start a Tcl/Tk script by simply invoking the script file itself ? cyu021 Programming 2 10-10-2004 12:00 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM


All times are GMT -5. The time now is 01:05 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration