LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-29-2005, 03:09 PM   #1
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Rep: Reputation: 15
Many files to edit with Kwrite, how to simplify?


I need to edit 1000+ php/html files,

the change for all of them is the same I need to change the header:

<head>
<title>CHANGE HERE</title>
</head>


Is there a way to write a script that would change all the header files of all the pages? or would I need to do it one at a time?


Other than kwrite, is there another editing program, that perhaps can edit all the files simultaneously?
 
Old 08-29-2005, 03:33 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
"Is there a way to write a script that would change all the header files of all the pages? "

Possibly. You would need a loop to find the file names one by one. Then for each file you could use sed to make the change. See:

man sed

-----------------------------
Steve Stites
 
Old 08-29-2005, 04:06 PM   #3
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Original Poster
Rep: Reputation: 15
Would something like this work?
sed {y/SOURCE/DEST} /www/html/change/*.*

where source is a text file containing only the header file I want to change(/home/source).
and dest is the complete html file. (home/dest)

Below are the commands I think I might need to use, but I'm not sure how to arrange them:

from the manual-
c \

text Replace the selected lines with text, which has each embedded
newline preceded by a backslash.

h H Copy/append pattern space to hold space.

g G Copy/append hold space to pattern space.

x Exchange the contents of the hold and pattern spaces.
__________

can someone give an example of how to properly use sed?
 
Old 08-29-2005, 05:57 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
This might get you started...
Code:
#!/bin/bash
mkdir /home/tmp 2> /dev/null
info="<TITLE>New Message Here</TITLE>"
for i in `ls *.html` ; do
   num=`cat $i | grep -n '<TITLE>[[:alnum:]*[:space:]]*</TITLE>' | cut -d: -f1`
   cat $i | sed -e ''$num'd' | sed -e ''$num'i\''\t'"$info"'' > /home/tmp/$i
done
 
Old 08-30-2005, 11:46 AM   #5
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Original Poster
Rep: Reputation: 15
thanks for the sample code!

I tried it, works ok if just using 1 file from my home directory... but when trying it in a folder with 1709 files, i get a bunch of 0 B files as output.

here's what's working:

#!/bin/bash
mkdir /home/mifan/tmp 2> /dev/null
info="<TITLE>你好</TITLE>"
for i in `ls *.php` ; do
num=`cat $i | grep -n '<TITLE>[[:alnum:]*[:space:]]*</TITLE>' | cut -d: -f1`
cat $i | sed -e ''$num'd' | sed -e ''$num'i''\t'"$info"'' > /home/mifan/tmp/$i
done


here's what's not working

#!/bin/bash
mkdir /home/mifan/TOCHANGE 2> /dev/null
info="<TITLE>你好</TITLE>"
for i in `ls *.php` ; do
num=`cat $i | grep -n '<TITLE>[[:alnum:]*[:space:]]*</TITLE>' | cut -d: -f1`
cat $i | sed -e ''$num'd' | sed -e ''$num'i''\t'"$info"'' > /home/mifan/FIXED/$i
done

I'm not too sure why though...
the script is located in the same folder as the files I wish to change.
to my understanding of this: num= is the script called by sed -e, info= is what will replace what's between the title, i'm not sure what the \t is for, it seems while its there it adds a "t" into the html files. also seems to work ok without it.
 
Old 08-30-2005, 12:49 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
It could be that <TITLE>[[:alnum:]*[:space:]]*</TITLE> won't work with php.
The \t was just to tab the title line over to be even with a sample html page I used.
Try this one
Code:
#!/bin/bash

mkdir /home/tmp 2> /dev/null
info="<TITLE>New Message Here</TITLE>"

for i in `ls *.php` ; do
   num=`cat $i | grep -n '<TITLE>[^>]*</TITLE>' | cut -d: -f1`
   cat $i | sed -e ''$num'd' | sed -e ''$num'i\''\t'"$info"'' > /home/tmp/$i
done
 
Old 08-31-2005, 08:06 AM   #7
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Original Poster
Rep: Reputation: 15
Right on! That worked great with php!

so what's the difference from php? why did [^>]* work for php and [:alnum:]*[:space:] for html?

I'm hoping to increase my knowledge on some of the more general programming/scripting languages :-)
 
Old 08-31-2005, 08:29 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Glad you got it going! :)

I don't know why php was fussy but using [^>] is neater and less typing anyway.
 
  


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
KWrite Backup Files (~) Kamex Linux - Software 2 07-26-2006 03:55 PM
.desktop files open with kwrite palmem Linux - Software 2 11-01-2004 02:32 PM
GDM faces and desktop -- trying to simplify sosiouxme Fedora 1 04-09-2004 08:45 PM
Header and footer of printed files (using Kwrite) Krappy Linux - Software 3 02-27-2004 08:21 PM
"Simplify! Simplify!" BillRice Linux - General 6 03-15-2002 01:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:53 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