LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-04-2016, 08: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, 08:33 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 08:49 PM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
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, 09: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, 09:16 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Fragile - what happens when $1 (in data) is not unique ?.
 
Old 07-04-2016, 09: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 09:37 PM.
 
  


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

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

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