LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-09-2006, 03:19 AM   #1
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Rep: Reputation: 15
Shell Script Commands


Hello!

Is there a command that would search for a text in a file and will return a certain value?

Thanks...
 
Old 02-09-2006, 03:49 AM   #2
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
grep

it returns what you search for. like all commands it also returns exit values that can be called from $?
 
Old 02-09-2006, 04:02 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
these are the return values for grep,

0 - match found ( could be one or more )
1- no match
2- other errors ( no such file )

i have encountered only the above three return values
 
Old 02-09-2006, 04:35 AM   #4
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
yep.

in a shell script you can call the last return value (from the last command) using $? e.g

awk '{ print $1}' /etc/passwd |grep someuser > /dev/null 2>&1

if test $? = 1
then
echo "this user does not exist"
fi



obviously that's a trite example, but it is useful.
 
Old 02-09-2006, 07:49 AM   #5
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Thank You satinet and kshkid!

In this line below:

awk '{ print $1}' /etc/passwd |grep someuser > /dev/null 2>&1

the file that im going to search is a log file which is /var/log/messages
But when I try it... It can't find any of words inside the log file eventhough it's in the log file. Why is that so?
 
Old 02-09-2006, 07:58 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
if u need to search with awk then,

Code:
awk ' /<searchword>/ {print}' /var/log/messages
 
Old 02-09-2006, 08:16 AM   #7
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
kshkid,

it would return this message...

awk: cmd. line:1: fatal: cannot open file `/var/og/messages' for reading (No such file or directory)
 
Old 02-09-2006, 08:23 AM   #8
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by shandy^^^
kshkid,

it would return this message...

awk: cmd. line:1: fatal: cannot open file `/var/og/messages' for reading (No such file or directory)
Is the /var/log/messages file available?

I could find a typo in the file name /var/og/messages

can you please post the command line entry that you had tried?
 
Old 02-09-2006, 08:37 AM   #9
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Yes, the /var/log/messages is the log file of the Fedora Core4.

This is the script, filename: file5

Code:
#!/bin/sh
awk '/<tezt>/ {print}' /var/log/messages

if test $? = 1
then
echo "this user does not exist"
else
echo "this exist"
fi
chmod 777 file5

./file5

and it would return "this exist" eventhoung the word i search "tezt" can not be found in the log file.
 
Old 02-09-2006, 08:51 AM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by shandy^^^
Yes, the /var/log/messages is the log file of the Fedora Core4.

This is the script, filename: file5

Code:
#!/bin/sh
awk '/<tezt>/ {print}' /var/log/messages

if test $? = 1
then
echo "this user does not exist"
else
echo "this exist"
fi
chmod 777 file5

./file5

and it would return "this exist" eventhoung the word i search "tezt" can not be found in the log file.
no shandy, this is not what i meant, sorry for the communication gap.

Code:
if [ `awk ' /tezt/ {print}' /var/log/messages | wc -l` -eq 0 ]
then
echo "no such user"
else
echo "user exists"
fi
 
Old 02-09-2006, 09:15 AM   #11
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
aaahhh yah its working already thanks...

do you know how to write a string into an existing text file?
 
Old 02-09-2006, 09:25 AM   #12
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
i cannot get your question,

do you mean replacing text,

or appending ?

can you please explain elaborately?
 
Old 02-09-2006, 09:38 AM   #13
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
i have two files: file1 and file2
i want the files of file1 to be copied to file2... as in it would overwrite the existing files in the file2

did you get?

when I use 'cat file1 >> file2'
and when i execute over and again the command it would just concantenate the files of file1 to file2 but not overwrite...
 
Old 02-09-2006, 09:40 AM   #14
shandy^^^
LQ Newbie
 
Registered: Jan 2006
Posts: 26

Original Poster
Rep: Reputation: 15
i have two files: file1 and file2
i want the files of file1 to be copied to file2... as in it would overwrite the existing files in the file2

did you get?

when I use 'cat file1 >> file2'
and when i execute over and again the command it would just concantenate the files of file1 to file2 but not overwrite...
 
Old 02-09-2006, 09:53 AM   #15
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
to append file1 contents to file2

cat file1 >> file2

to overwrite file2 contents with file1

cat file1 > file2
or
mv file1 file2
or
cp file1 file2

Last edited by kshkid; 02-09-2006 at 09:54 AM.
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Commands/Shell script ?? paraiso Linux - Newbie 11 04-21-2005 10:58 AM
Commands from shell script not working Grassie Coetzee Linux - Software 2 03-13-2005 02:31 PM
Use a shell script to do login and other commands jpan Linux - General 15 01-12-2005 06:52 AM


All times are GMT -5. The time now is 11:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration