Help printing string

Hi,

I have a file containing many times the word "IPaddress". and i need a command that finds that string or word and print it only once. I've tried the following but they print a line for each match:

grep "IPaddress" /directory/file.xml
find . | xargs grep 'IPaddress' -sl

i have many others but i get the same results.

Having trouble understanding what you want, here are a couple of examples

This should print out the path of any file containing your string, note I used -type f to avoid looking at directories or special files and the -print0 and -0 args are to support files/directories with spaces -r stops grep being run if no files exist under path (ie find did not match any files):

find . -type f -print0 | xargs -0 -r grep -l 'IPaddress'

This will print "IPaddress" if the given file contains the string

grep -q "IPaddress" /directory/file.xml && echo "IPaddress"

As Chubler_XL said, your requirement doesn't seem very clear.

If you wanted to print the contents of the first matching line from a file you could do:

 awk '/IPaddress/ {print; exit}' file

Or if you have GNU grep:

grep -m1 IPaddress file