LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-28-2011, 01:05 AM   #1
Fosters
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Grep help


I have a file A:
With say
111
222
333

And a file b with:
444:aaabbcc
555:ddeeff
111:ccccsd
I want to output lines in file b which start with the lines in file a.

How do I do this?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-28-2011, 01:16 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,861
Blog Entries: 36

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
Sounds like hw so I'll point you in the right direction.

You need to read the file into a loop. That should be more than enough to get you started.

Also read the man documentation for grep.
Code:
man grep
---

side note: If there's a legitimate reason as to why you want to do this other than HW then describe us your actual problem instead of what you think should be the solution. There might be a better way to accomplish what you want.

Last edited by sag47; 10-28-2011 at 01:19 AM.
 
Old 10-28-2011, 01:36 AM   #3
Fosters
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
while read line; do grep "$line" file1.txt; done < file2.txt

Hello, thanks for your help.

I tried the above, but it fails, can you please tell me whats wrong?

Thanks.

It is HW.

~Courtney
 
Old 10-28-2011, 01:56 AM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,861
Blog Entries: 36

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
Are you sure you don't have file1 and file2 switched? Because you're using the contents of file a (assuming file1.txt) to grep the contents of file b (assuming file2.txt).

Last edited by sag47; 10-28-2011 at 02:06 AM.
 
Old 10-28-2011, 01:57 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Who needs a script?

grep -f "file A" "file b"



Cheers,
Tink
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 03:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:28 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