LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find a match string in conf file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-a-match-string-in-conf-file-662157/)

Ameii83 08-12-2008 01:51 AM

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??

prakash.akumalla 08-12-2008 01:57 AM

Hi,
You can try cat filename | grep string

Prakash.

chrism01 08-12-2008 01:59 AM

UUOC (useless use of cat)

Try

grep string filename

Ameii83 08-12-2008 02:36 AM

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

colucix 08-12-2008 03:04 AM

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?

Ameii83 08-12-2008 04:40 AM

remove a whole string using keyword
 
Quote:

Originally Posted by colucix (Post 3244525)
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

chrism01 08-12-2008 07:42 PM

Try

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

(untested)


All times are GMT -5. The time now is 04:16 AM.