LinuxQuestions.org
Help answer threads with 0 replies.
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 02-28-2011, 10:13 PM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
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?
 
Old 03-01-2011, 01:20 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 03-01-2011, 01:23 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 03-01-2011 at 01:32 AM. Reason: as above
 
1 members found this post helpful.
Old 03-04-2011, 07:02 AM   #4
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
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.

Last edited by tkmsr; 03-04-2011 at 07:04 AM.
 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 06:36 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
linux 'find' to find text files subu_s Programming 2 12-02-2004 02:56 AM

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

All times are GMT -5. The time now is 06:07 AM.

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