LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script: find, grep if and else programing.. (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-find-grep-if-and-else-programing-839288/)

snaaps 10-20-2010 09:32 AM

bash script: find, grep if and else programing..
 
Hi,

I am bussy with a litle bash script but i have now a problem.

I have a file on the server with every time different text.
Somewere in this text the is the following line:

PHP Code:

<BR><DIV CLASS='itemTotalsTitle'>2 Matching Service Entries Displayed</DIV

I want to make a bash script that replace this line when it says:
"0 Matching Service Entries Displayed"
To a other text like:
"There a no knowing problem(s) on this moment."]

If there is a other number than "0" than replace this line with:
2 problems have been found on this moment, whe are bussy to fix this problem, please be paitient.


Hope that somewone can help me..

hairysocks 10-20-2010 10:24 AM

If you use grep -l to match the files that contain the text you want, then use the filename that grep gives you and replace the text in the file.

Your text is not quite correct English, you should say:

"There are no known problems at this moment."

and:

"2 problems have been found, we are busy fixing this problem, please be patient"

Snark1994 10-20-2010 12:00 PM

I think you're looking for something like this...

Code:

#!/bin/bash

mv filename tmpFile
sed -e "s/\([1-9][0-9]\+\) Matching Service Entries Displayed/\1 problems have been found; we are busy fixing these problems, so please be patient./" \
    -e "s/0 Matching Service Entries Displayed/There are no known problems at the moment/" \
    tmpFile 1> filename
rm tmpFile

Obviously, replace filename with the actual name of the file. sed doesn't seem to like to write to the file it's reading from (there's probably a proper way to do it which I've missed), but just moving the file to a temporary file then writing back seems to work...

Hope this helps :)

grail 10-20-2010 08:24 PM

Assuming 'itemTotalsTitle' is a unique item, here is a variation:
Code:

#!bin/bash

NO_PROB="There are no known problems at this moment."
PROB=" problems have been found, we are busy fixing this problem, please be patient"

sed -i.bak -r "/itemTotalsTitle/{/0/s/0[^<]+/$NO_PROB/;s/([1-9][0-9]*)[^<]+/\1$PROB/}" input_file

I find the variables make it a little clearer and -i.bak will create a backup in case things go wrong :)


All times are GMT -5. The time now is 12:54 AM.