LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-04-2016, 09:22 PM   #1
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Rep: Reputation: Disabled
How to print the lines that contains certain strings by order?


I have two files

file indv


COPDGene_P51515
COPDGene_V67803
COPDGene_Z75868
COPDGene_U48329
COPDGene_R08908
COPDGene_E34944
file data

COPDGene_Z75868 1
COPDGene_A12318 3
COPDGene_R08908 5
COPDGene_P51515 8
COPDGene_U48329 2
COPDGene_V67803 8
COPDGene_E34944 2
COPDGene_D29835 9

I want to print the lines that contains the strings in the `indv` by the order of `indv` like following

COPDGene_P51515 8
COPDGene_V67803 8
COPDGene_Z75868 1
COPDGene_U48329 2
COPDGene_R08908 5
COPDGene_E34944 2

I tried to use

Code:
awk 'NR==FNR{a[$1]++;next} ($1 in a)' indv data
But I got

COPDGene_Z75868 1
COPDGene_R08908 5
COPDGene_P51515 8
COPDGene_U48329 2
COPDGene_V67803 8
COPDGene_E34944 2

which is not the order of `indv`.
 
Old 07-04-2016, 09:33 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,033

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Quote:
Originally Posted by awk_manual
By default, the order in which a ‘for (i in array)’ loop scans an array is not defined; it is generally based upon the internal implementation of arrays inside awk.
You will have to design your own solution if neither the index or data is not in some natural order. I have used 2 dimensional array for this sort type of thing.
 
Old 07-04-2016, 09:49 PM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,461

Rep: Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368
Perhaps a simple loop involving grep would work.
Code:
while read aline; do grep "$aline" file.txt; done < indv.txt
 
1 members found this post helpful.
Old 07-04-2016, 10:06 PM   #4
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Code:
awk 'NR==FNR{a[$1]=$2;next} a[$1]{print $1,a[$1]}' data indv
Output:
Code:
COPDGene_P51515 8
COPDGene_V67803 8
COPDGene_Z75868 1
COPDGene_U48329 2
COPDGene_R08908 5
COPDGene_E34944 2
 
Old 07-04-2016, 10:16 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,033

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Fragile - what happens when $1 (in data) is not unique ?.
 
Old 07-04-2016, 10:32 PM   #6
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by syg00 View Post
Fragile - what happens when $1 (in data) is not unique ?.
Why to call it fragile when no indication has been made for your requirement? If that were the case a more laborious approach must be taken, since the last entry will have precedence. However, as stand, until that requirement is made my suggestion is perfectly solid and meets the requested output.

Last edited by Aia; 07-04-2016 at 10:37 PM.
 
  


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
BASH: rearrange lines in a file by line number & print in that order SilversleevesX Linux - Newbie 7 09-20-2011 08:31 PM
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 2 12-06-2009 07:57 AM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-27-2009 12:29 AM
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-07-2008 12:57 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 09:25 AM


All times are GMT -5. The time now is 10:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration