LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I program in shell script for write a word if it finds another word (http://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-program-in-shell-script-for-write-a-word-if-it-finds-another-word-941225/)

bal_nair1 04-23-2012 12:27 AM

How do I program in shell script for write a word if it finds another word
 
I have a file named medpre.msg.log
Script should search for a word 'degenerated'
If it finds the word 'degenerated' , then it should write 'ECHT' on already existing file Mcheck.prot

How should I write the program, please help me . I am new to shell scripting


Thank you in advance......:)

chrism01 04-23-2012 01:03 AM

Please show us what you have done so far and we'll point you in the right direction.
In the meantime, here are some useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Have a look at the grep cmd http://linux.die.net/man/1/grep

bal_nair1 04-23-2012 01:19 AM

Sorry I have not done anything so far , I need help extensively to do this program..


I have a file medpre.msg.log in the path /home/balnair/.medina/medpre.msg.log

and another file in the path /home/transfer/Mcheck.prot

script should search for the word 'degenerated' in medpre.msg.log

If it finds it should append 'ECHT' to file Mcheck.prot



So how can be done
log_file = " /home/balnair/.medina/medpre.msg.log "
work = "/home/transfer/Mcheck.prot"


if [ !"degenerated" = $log_file ] # please correct it
then
echo " ECHT " >>$work

pan64 04-23-2012 01:45 AM

see the man page of grep, it will help you

bal_nair1 04-23-2012 02:07 AM

ok i have put

find = " grep degenerated $logfile | wc -l "

if [ $find >1]

then
echo " ECHT " >> $work


but it doesnt work.......

druuna 04-23-2012 02:14 AM

Hi,

Give this a try:
Code:

if [ $( grep "degenerated" "$logfile" ) ]
then
  echo "ECHT" >> "$work"
fi

Hope this helps.

grail 04-23-2012 02:16 AM

Explain what is not working? Also, what shell are you using?

pan64 04-23-2012 02:43 AM

more simple: grep degenerated $logfile && echo ECHT >> "$work"

---------- Post added 23rd Apr 2012 at 09:44 ----------

Quote:

Originally Posted by grail (Post 4660429)
Explain what is not working? Also, what shell are you using?

he should add a space before ]

grail 04-23-2012 06:04 AM

Quote:

he should add a space before ]
So that is the only error you see? Remember, just telling someone the answer won't really help them much for next time.

pan64 04-23-2012 06:28 AM

Quote:

Originally Posted by grail (Post 4660619)
So that is the only error you see? Remember, just telling someone the answer won't really help them much for next time.

If someone wants to learn linux he would better solve such problems himself. So yes, I will show him a starting point. If there were other errors he should find them easily there are only 5 lines of codes to check.


All times are GMT -5. The time now is 12:32 PM.