LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-31-2005, 10:51 AM   #1
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Rep: Reputation: 15
sed is a useful tool. can it do this?


So far I'm learning that sed and grep can do quite a bit, but I'd like to know if the following is possible:

using sed or grep, can I compare text from a .doc file to a folder containing html files to see if a particular paragraph exists in one of the html files?

I think its a possibility, but I'm not sure exactly.


Earlier, I got help to rename jpg files, php files, but now, is it possible to use sed to change the links inside php files to point to the newly renamed files?

for example, I want to change the <a href=mypage1,php> to <a href mypage001.php>
so that the link mypage002.php points to mypage001.php (previously they were mypage2.php and mypage1.php)

is there a way for sed to change something sequentially?


this was the script homey helped me with to change the title:

#!/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


is there a way to modify it? perhaps like this:

#!/bin/bash

mkdir /home/tmp 2> /dev/null
info="<a href=file{SEQUENTIAL NUMBER GOES HERE}.php>"

for i in `ls *.php` ; do
num=`cat $i | grep -n '<a href=[^>]*>' | cut -d: -f1`
cat $i | sed -e ''$num'd' | sed -e ''$num'i''\t'"$info"'' > /home/tmp/$i
done
 
Old 08-31-2005, 12:38 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
So basically, the part about mypage1.php and mypage2.php is correct but you just want two zeros in front of the number?
 
Old 08-31-2005, 03:38 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Here is something I've been putzing with. Is this what you have in mind?
Code:
#!/bin/bash
mkdir /home/tmp 2> /dev/null
for i in `ls *.php` ; do
n=$(( $n + 1 ))

if [ $n -lt '10' ]; then
  cat $i | sed -e 's/mypage[0-9]*.php/mypage'00$n.php'/g' > /home/tmp/$i
elif [ $n -ge '10' -a $n -lt '100' ]; then
   cat $i | sed -e 's/mypage[0-9]*.php/mypage'0$n.php'/g' > /home/tmp/$i
else cat $i | sed -e 's/mypage[0-9]*.php/mypage'$n.php'/g' > /home/tmp/$i
fi
done

Last edited by homey; 08-31-2005 at 03:41 PM.
 
Old 08-31-2005, 06:37 PM   #4
Earthwings
LQ Newbie
 
Registered: Aug 2005
Location: Karlsruhe, Germany
Distribution: Gentoo, Kubuntu
Posts: 20

Rep: Reputation: 0
I'm not sure what you are trying to achieve with $n.
Let me add some smartass comments on scripting style, improving it can save lots of time and trouble.

mkdir /home/tmp 2> /dev/null
should better be
mkdir -p /home/tmp || exit 1
The -p switch makes mkdir create parent directories if necessary and doesn't give an error when the directory already exists. As you rely on /home/tmp later in the script, you should exit in case mkdir errors out, this could be due to /home/tmp being a regular file.

for i in `ls *.php` ; do
is quite dangerous in case you've got files containing spaces, bash would parse that as two different non-existing "files". Use
for i in *.php ; do
instead.

n=$(( $n + 1 ))
can be shortened to
$((n++))

I'm not sure about the next section. You could use sed <parameters> <file> instead of [i]cat <file> | sed <parameters</i] however. Additionally use ${var} instead of $var in case $var is used in a string like foo$varbar. foo${var}bar is unambigous, while foo$varbar could match $var, $varb, $varba, $varbar.

Here's a modified sed call that processes everything at once.

Code:
sed -e 's/mypage\([0-9]\{1\}\)\.php/mypage00\1.php/g' \
-e 's/mypage\([0-9]\{2\}\)\.php/mypage0\1.php/g' \
-e 's/mypage\([0-9]\{3\}\)\.php/mypage\1.php/g' $i
The \1 in the sed calls are backreferences which get replaced by the number matched before.
\ indicates a new line
 
Old 09-01-2005, 11:37 AM   #5
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Original Poster
Rep: Reputation: 15
Ok, lets see if I understand this right:

the s/ will separate the mypage from the number, i thing '00$n.php'/g means to hold this pattern?
but I'm not sure where the replacement will actually take place... does it happen with the command /g ?

#!/bin/bash
mkdir /home/tmp 2> /dev/null
for i in `ls *.php` ; do
n=$(( $n + 1 ))

if [ $n -lt '10' ]; then
cat $i | sed -e 's/mypage[0-9]*.php/mypage'00$n.php'/g' > /home/tmp/$i
elif [ $n -ge '10' -a $n -lt '100' ]; then
cat $i | sed -e 's/mypage[0-9]*.php/mypage'0$n.php'/g' > /home/tmp/$i
else cat $i | sed -e 's/mypage[0-9]*.php/mypage'$n.php'/g' > /home/tmp/$i
fi
done


this piece of code is also interesting:
sed -e 's/mypage\([0-9]\{1\}\)\.php/mypage00\1.php/g' \
-e 's/mypage\([0-9]\{2\}\)\.php/mypage0\1.php/g' \
-e 's/mypage\([0-9]\{3\}\)\.php/mypage\1.php/g' $i
i'm not sure exactly but I think the[0-9]\[1\] reffers to the number off zeros to add? ie 1 =0001, 2=0010, 3=0100, etc.

anyway, i tried running the scripts and got this error:

: bad interpreter: No such file or directory


I modified the file as follows:

#!/bin/bash
mkdir /home/mifan/tmp 2> /dev/null
for i in `ls *.php` ; do
n=$(( $n + 1 ))

if [ $n -lt '10' ]; then
cat $i | sed -e 's/file_[0-9]*.php/file_'00$n.php'/g' > /home/mifan/tmp/$i
elif [ $n -ge '10' -a $n -lt '100' ]; then
cat $i | sed -e 's/file_[0-9]*.php/file_'0$n.php'/g' > /home/mifan/tmp/$i
else cat $i | sed -e 's/file_[0-9]*.php/file_'$n.php'/g' > /home/mifan/tmp/$i
fi
done

and I also tried:

#!/bin/bash
mkdir -p /home/mifan/tmp || exit 1
for i in *.php ; do
$((n++))

if [ $n -lt '10' ]; then
sed -e 's/mypage\([0-9]\{1\}\)\.php/mypage00\1.php/g' \
-e 's/mypage\([0-9]\{2\}\)\.php/mypage0\1.php/g' \
-e 's/mypage\([0-9]\{3\}\)\.php/mypage\1.php/g' $i
fi
done

and got the same error
 
Old 09-01-2005, 12:39 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Quote:
: bad interpreter: No such file or directory
That probably means that you made / edited the script with a dos based editor like notepad.
You shouldn't have that problem if you use a linux editor such as kwrite.

You could also fix your existing efforts by running this command. Note: Change the file.txt to whatever you have naned the script.
Code:
tr -d '\015' file.txt > file1.txt
 
  


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
sed benobi Linux - Newbie 8 05-17-2005 04:44 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
URLSCAN tool MS = Linux tool ? OB1 Linux - Security 3 10-05-2002 01:58 AM
Sed help noodle123 Programming 6 04-22-2002 10:08 AM


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