LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-21-2010, 07:13 AM   #1
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Rep: Reputation: 0
Need help in linux command


Hello,

I have a text file with some IDs something like this:

PF0001.14
PF0002.17
PF0003.2

I want to go through this IDs and for each one I want to search the corresponding line in which the line has the particular id which are stored in an another text file, which is:

A0T2N3#PF00001.14;51-316;#
A2ARI4#PF00001.14;555-801;#PF01462.11;29-56;#PF00560.26;59-80;130-152;154-176;226-247;344-364;390-412;#
A6QPC8#PF08743.3;81-378;#
A6NET4#PF00001.14;41-288;#
A6QPB3#PF01391.11;567-631;593-646;743-802;812-861;909-950;1417-1465;#
A6QPF4#PF04103.8;154-273;#
A6QPH8#PF03226.7;6-115;#
A6QLU6#PF00002.17;591-830;#PF01825.14;535-579;#
A6QPF8#PF07851.6;7-337;#
A6QPH1#PF09282.3;11-37;#
A6QP74#PF00002.17;139-381;#PF02793.15;63-131;#

below is the desired result:

A0T2N3#PF00001.14;51-316;#
A2ARI4#PF00001.14;555-801;#PF01462.11;29-56;#PF00560.26;59-80;130-152;154-176;226-247;344-364;390-412;#
A6NET4#PF00001.14;41-288;#

--------------------------------------------------

A6QLU6#PF00002.17;591-830;#PF01825.14;535-579;#
A6QP74#PF00002.17;139-381;#PF02793.15;63-131;#

---------------------------------------------------------


I would be really glad for any help.

Thanks
Riya
 
Old 12-21-2010, 07:49 AM   #2
mesiol
Member
 
Registered: Nov 2008
Location: Lower Saxony, Germany
Distribution: CentOS, RHEL, Solaris 10, AIX, HP-UX
Posts: 731

Rep: Reputation: 137Reputation: 137
Hi,

try
Code:
man grep
and use a bash
Code:
man bash
to learn about for construct.
 
Old 12-21-2010, 08:01 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Expanding a little on what mesiol wrote, you could write a bash script which loops (a while read loop is suitable) on each line of the first file and greps the second file for the contents of each line read. Many examples from LQ here.
 
1 members found this post helpful.
Old 12-21-2010, 08:31 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You don't really need a loop by using grep's option -f.
 
Old 12-21-2010, 06:27 PM   #5
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,656
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
Nice search param. catkin

"while read" site:linuxquestions.org

I am still learning. I guess I'll never stop

cheers Glenn
 
Old 12-21-2010, 09:17 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
And nice grep option, colucix I guess I'll never stop learning either.
 
Old 12-21-2010, 10:59 PM   #7
dharanivasan
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Rep: Reputation: 1
grep [-f FILE] [FILE...]
Where -f will be pattern file.
 
Old 12-31-2010, 03:34 AM   #8
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Original Poster
Rep: Reputation: 0
THANK YOU FOR YOUR REPLY
HI DHARANIVASAN I TRIED WITH THE BELLOW GIVEN COMMAND BUT ITS NOT WORKING

grep -f patternfile file2 >output.txt

CAN YOU PLEASE HELP ME....
 
Old 12-31-2010, 04:48 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Priyabio View Post
CAN YOU PLEASE HELP ME....
Probably not without more information.

What were the symptoms of "BUT ITS NOT WORKING"? Copying from the command prompt session and pasting into a post in this thread would be neat.
 
Old 01-01-2011, 07:38 AM   #10
Priyabio
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hi

I am not getting the desired output by using the command grep -f patternfile file2 >output.txt
I got the same input file as the output...

The desired output is given below

A0T2N3#PF00001.14;51-316;#
A2ARI4#PF00001.14;555-801;#PF01462.11;29-56;#PF00560.26;59-80;130-152;154-176;226-247;344-364;390-412;#
A6NET4#PF00001.14;41-288;#

--------------------------------------------------

A6QLU6#PF00002.17;591-830;#PF01825.14;535-579;#
A6QP74#PF00002.17;139-381;#PF02793.15;63-131;#

---------------------------------------------------------
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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