LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-20-2010, 10:32 AM   #1
snaaps
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
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..
 
Old 10-20-2010, 11:24 AM   #2
hairysocks
Member
 
Registered: May 2002
Location: Thorverton, Near Exeter, Devon, England
Distribution: Ubuntu 10.04 (used to be Red Hat 7.1, then Red Hat 9, then FC 2, FC 5, FC 6, FC 9 and Ubuntu 8.04)
Posts: 105

Rep: Reputation: 16
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"
 
Old 10-20-2010, 01:00 PM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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

Last edited by Snark1994; 10-20-2010 at 01:05 PM. Reason: Reducing code's line length, and removing a bug :)
 
Old 10-20-2010, 09:24 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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
 
  


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
[SOLVED] grep for variable in a bash script wick_chad Programming 6 08-20-2010 11:16 AM
[SOLVED] Need help in bash script and grep manya Programming 24 05-07-2010 08:06 AM
bash script grep without double saavik Programming 2 05-14-2009 11:15 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
bash script and grep syros Programming 4 01-13-2005 04:04 PM


All times are GMT -5. The time now is 06:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration