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 |
So basically, the part about mypage1.php and mypage2.php is correct but you just want two zeros in front of the number?
|
Here is something I've been putzing with. Is this what you have in mind?
Code:
#!/bin/bash |
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' \ \ indicates a new line |
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 |
Quote:
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 |
All times are GMT -5. The time now is 05:05 PM. |