LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using find or grep to find a group of text strings. (http://www.linuxquestions.org/questions/linux-newbie-8/using-find-or-grep-to-find-a-group-of-text-strings-865624/)

tkmsr 02-28-2011 10:13 PM

using find or grep to find a group of text strings.
 
I have used diff command in past.I faced a situation to which I did not had a clue
here are some text strings (which can be stored in a file)

Quote:

CONFIG_XEN=y
CONFIG_XEN_PVHVM=y
CONFIG_XEN_MAX_DOMAIN_MEMORY=128
CONFIG_XEN_SAVE_RESTORE=y
CONFIG_XEN_DEBUG_FS=y
CONFIG_SWIOTLB_XEN=y
CONFIG_MICROCODE_XEN=y
CONFIG_XEN_DOM0=y
CONFIG_XEN_PRIVILEGED_GUEST=y
CONFIG_XEN_DOM0_PCI=y
CONFIG_XEN_PCI_PASSTHROUGH=y
CONFIG_PCI_XEN=y
CONFIG_XEN_PCIDEV_FRONTEND=y
CONFIG_XEN_BLKDEV_FRONTEND=y
CONFIG_NETXEN_NIC=m
CONFIG_XEN_NETDEV_FRONTEND=m
CONFIG_XEN_KBDDEV_FRONTEND=y
CONFIG_HVC_XEN=y
CONFIG_XEN_FBDEV_FRONTEND=y
CONFIG_XEN_BALLOON=y
CONFIG_XEN_SCRUB_PAGES=y
CONFIG_XEN_DEV_EVTCHN=y
CONFIG_XEN_BACKEND=y
CONFIG_XEN_NETDEV_BACKEND=m
CONFIG_XEN_BLKDEV_BACKEND=m
CONFIG_XEN_BLKDEV_TAP=m
CONFIG_XEN_BLKBACK_PAGEMAP=m
CONFIG_XEN_PCIDEV_BACKEND=m
CONFIG_XEN_PCIDEV_BACKEND_VPCI=y
CONFIG_XENFS=y
CONFIG_XEN_COMPAT_XENFS=y
CONFIG_XEN_SYS_HYPERVISOR=y
CONFIG_XEN_MCE=y
CONFIG_XEN_XENBUS_FRONTEND=y
CONFIG_XEN_GNTDEV=y
CONFIG_XEN_S3=y
CONFIG_ACPI_PROCESSOR_XEN=y
CONFIG_XEN_PLATFORM_PCI=m
I have to basically find above strings only (not any other) in a .config file of kernel which looks as follows

http://pastebin.com/AEQ6p9Vm
It is a very big file.

Now I had no clue if by commands I can find the entries I first mentioned whether exist in second file or not so I did manually copied each entry and searched in the .config I mentioned.I found

there are following differences

Quote:

# CONFIG_XEN_DEBUG_FS is not set CONFIG_XEN_BLKBACK_PAGEMAP <--- is
completely missing
# CONFIG_XEN_NETDEV_BACKEND is not set
# CONFIG_XEN_BLKDEV_BACKEND is not set
# CONFIG_XEN_BLKDEV_TAP is not set CONFIG_XENFS=y
# CONFIG_XEN_GNTDEV is not set
Can this result be easily done by find grep or some thing similar?

grail 03-01-2011 01:20 AM

Well find is used on a file system, ie not on individual files for their contents.

Grep would not supply the information on its own but could be used in conjunction with a bash loop, probably while would be my choice.

Or alternatively you could probably construct an awk or perl or some such script to deliver the information.

David the H. 03-01-2011 01:23 AM

Code:

grep "textstring" filename.
grep is for searching for strings or patterns of text in a file.

find is for locating files with specified names or other attributes on the system.

Start with man grep and man find. Then hop onto google and do some searching. There's tons of info out there about how to use tools like this.

Edit: Here's an easy way to search for the lines in one file that exist in a second file:
Code:

grep -n -F -f fileB.txt fileA.txt
-n prefixes the line number
-F searches for fixed strings instead of regex patterns
-f specifies a file to take the strings/patterns from, instead of on the command line.

So in this case, search fileA.txt for the lines stored in fileB.txt, and print them along with their line number.

tkmsr 03-04-2011 07:02 AM

Hi David H. Thanks for your pointers.I basically did read the man pages but -F option was not clear to me.
What I did is used your way

Quote:

grep -n -F -f fileB.txt fileA.txt > result.txt
then
Quote:

diff --suppress-common-lines fileB.txt result.txt > result2.txt
then
Quote:

grep -v -f fileB.txt result2.txt
and then went to edit the entries in fileA.txt.


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