LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-12-2008, 01:51 AM   #1
Ameii83
Member
 
Registered: Dec 2004
Location: malaysia
Posts: 56

Rep: Reputation: 15
Question How to find a match string in conf file


Heloo guys

I got one file test.conf. How i 'm going to find a string e.g "ServerID" inside test.conf using command line??
 
Old 08-12-2008, 01:57 AM   #2
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Rep: Reputation: 15
Hi,
You can try cat filename | grep string

Prakash.
 
Old 08-12-2008, 01:59 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
UUOC (useless use of cat)

Try

grep string filename
 
Old 08-12-2008, 02:36 AM   #4
Ameii83
Member
 
Registered: Dec 2004
Location: malaysia
Posts: 56

Original Poster
Rep: Reputation: 15
Question Spec file script to check the string

thank your for the quick reply. Both solution is working finee

I'm now trying to build a rpm file.In my spec file, i would like to check the "serverID" string it exist or not in my "test.conf" file.
Now, i 'm wondering how to give a return true status if "serverID" string is exist on my test.conf file using the solution above.Then it will remove the string
 
Old 08-12-2008, 03:04 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using an if-then test, you can evaluate the exit status of a command. For example:
Code:
if grep string filename
then
  <do something here>
fi
note there aren't parentheses around the test condition, since it evaluates a command, not an expression. However, you can accomplish your task in a single step using sed, for example
Code:
sed 's/string_to_delete//g' filename
that is you substitute the string to delete with nothing. The command above send the output (that is the modified content of the file) to the standard output. To edit the file in place you have to add the -i option.

The problem is to choose a regexp that matches only the string you want to delete. Can you provide a real example of what should be the content (or a relevenat part of it) of the file and the string to match?
 
Old 08-12-2008, 04:40 AM   #6
Ameii83
Member
 
Registered: Dec 2004
Location: malaysia
Posts: 56

Original Poster
Rep: Reputation: 15
Question remove a whole string using keyword

Quote:
Originally Posted by colucix View Post
Using an if-then test, you can evaluate the exit status of a command. For example:
Code:
if grep string filename
then
  <do something here>
fi
note there aren't parentheses around the test condition, since it evaluates a command, not an expression. However, you can accomplish your task in a single step using sed, for example
Code:
sed 's/string_to_delete//g' filename
that is you substitute the string to delete with nothing. The command above send the output (that is the modified content of the file) to the standard output. To edit the file in place you have to add the -i option.

The problem is to choose a regexp that matches only the string you want to delete. Can you provide a real example of what should be the content (or a relevenat part of it) of the file and the string to match?

Thanks for your reply.It give me a more clear idea how to this.
Using the "sed" command, in my test.conf
i got this string "Serverid=[N]"
actually the N is variable and it's value are not fixed

Code:
sed -i 's/ServerID//g' filename
.

It only remove "ServerID".Test conf output will be "=[n]".
How i'm going to remove the whole string by defining "ServerID" only
 
Old 08-12-2008, 07:42 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try

sed -i 's/ServerID.*//' filename

(untested)
 
  


Reply



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
how to find out particular string in a file mksc Linux - Newbie 1 06-23-2008 03:04 AM
find a string in all ascii file of a SOLARIS system markraem Solaris / OpenSolaris 4 01-16-2008 05:58 PM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
iptables string match kahpeetan Linux - Security 3 11-09-2003 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:55 PM.

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
Open Source Consulting | Domain Registration