Find common and fetch what ever in front of it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find common and fetch what ever in front of it
# 1  
Old 11-08-2012
Find common and fetch what ever in front of it

Hi guys

As my previous one is not working now I have a different problem with me


one file with entries
Code:
F2
F3
YUH
SUH

second fiel several columns like excel sheet (8-9) but my file in text

Code:
F2                  fgf             gfhjhjghjghj         dhgfhgfh   234324
F3                  dfghd          sdfhdhg                 sgrffg       476
X4                  dfhg                gfhjhjghjghj         dhgfhgfh   2343
H8                    dfghd          sdfhdhg                 sgrffg       476
YUH              dfghd          sdfhdhg                 sgrffg       476
SUH                 dfghd          sdfhdhg                 sgrffg       476

so if there is common fetch all columns in front of it as it is expeceted ouptut

Code:
F2                  fgf             gfhjhjghjghj         dhgfhgfh   234324
 F3                  dfghd          sdfhdhg                 sgrffg       476
YUH              dfghd          sdfhdhg                 sgrffg       476
 SUH                 dfghd          sdfhdhg                 sgrffg

# 2  
Old 11-08-2012
If the files are sorted then use join

Code:
join file1 file2

if not sorted then use awk

Code:
awk 'NR==FNR{X[$0]++;next}{if(X[$1]){print}}' file1 file2

# 3  
Old 11-08-2012
Thanks! but.....

using second code output is blank as files are not sorted


and using first code it is giving me only common entries of first column
# 4  
Old 11-08-2012
Smilie....

I believe this is what you asked for..

Code:
$ cat file1
F2
F3
YUH
SUH

Code:
$ cat file2
F2                  fgf             gfhjhjghjghj         dhgfhgfh   234324
F3                  dfghd          sdfhdhg                 sgrffg       476
X4                  dfhg                gfhjhjghjghj         dhgfhgfh   2343
H8                    dfghd          sdfhdhg                 sgrffg       476
YUH              dfghd          sdfhdhg                 sgrffg       476
SUH                 dfghd          sdfhdhg                 sgrffg       476

Code:
$ join file1 file2
F2 fgf gfhjhjghjghj dhgfhgfh 234324
F3 dfghd sdfhdhg sgrffg 476
YUH dfghd sdfhdhg sgrffg 476
SUH dfghd sdfhdhg sgrffg 476

Code:
$ awk 'NR==FNR{X[$0]++;next}{if(X[$1]){print}}' file1 file2
F2                  fgf             gfhjhjghjghj         dhgfhgfh   234324
F3                  dfghd          sdfhdhg                 sgrffg       476
YUH              dfghd          sdfhdhg                 sgrffg       476
SUH                 dfghd          sdfhdhg                 sgrffg       476

pamu
# 5  
Old 11-08-2012
I think my inputs are more compelx than sample

here are attached files for these ones...

BD(first file)

BD new(second file)

I take these files forme excel and paste into text to run program am I doing soemthign wrong?

Please advise me!
# 6  
Old 11-08-2012
Quote:
Originally Posted by Priyanka Chopra
I think my inputs are more compelx than sample
I take these files forme excel and paste into text to run program am I doing soemthign wrong?
Please try to explain as much you can. What you want.

I am assuming you want content of file2 if it matches with column 1 of file1

Code:
awk 'NR==FNR{X[$1]++;next}{if(X[$1]){print}}' file1 file2

Please let me know if you want to add anything here..

pamu
# 7  
Old 11-08-2012
When you use EXCEL files I bet there's strange DOS/Windows style control characters in there torpedoing unix like solutions like pamu's. Pls get rid of those exotic chars first. Search this forum; there's many a thread dealing with DOS char removal.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common words

Hi, I have 10 files which needs to be print common words from those all files. Is there any command to find out. (2 Replies)
Discussion started by: munna_dude
2 Replies

2. Shell Programming and Scripting

Find the common values

Hi, I have two files with the below values. file1 305231921 1.0 ben/Ben_Determination_Appeals 1348791394 2.0 ben/Ben_Determination_Appeals] 1305231921 1.0 ben/Cancel_Refund_Payment_JLRS 1348791394 2.0 ben/Cancel_Refund_Payment_JLRS 1305231921 ... (2 Replies)
Discussion started by: Vikram_Tanwar12
2 Replies

3. Shell Programming and Scripting

Match first column entries precisely and fetch whatever in front of it

Hi all I have 2 files: first file AABC TTYP JKBH CVBN NHJK KJHM Second file is AABC,XCYU,JUHD Alllele1 GACXT It is approved study TTYP,JKBH Allele2 PPRD It is clinical trial study JKBH Allele2 PPRD ... (5 Replies)
Discussion started by: Priyanka Chopra
5 Replies

4. Shell Programming and Scripting

Match columns and fetch whatever in front of it

Hi Solved these kind of issues using these codes But these are not wrking for my attached files can anybody check........ awk 'NR==FNR{X++;next}{if(X){print}}' file1 file2 awk 'NR==FNR{X=$0;next}{n=split($1,P," ");sub($1,"",$0);for(i=1;i<=n;i++){if(X]){print P,$0}}}' file1 FS="\t" file2 ... (6 Replies)
Discussion started by: Priyanka Chopra
6 Replies

5. Shell Programming and Scripting

Find common entries

Hi all I have to compare two files and find common entries First file is like this XVY CVY ZYN MNA In second file I have to search these entries in even number of columns 5 XVY 7 hdfj 8 CVY 9 if there is common entries then out put shuld be 5 XVY(approved) 7 hdfj 8... (11 Replies)
Discussion started by: manigrover
11 Replies

6. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

7. Shell Programming and Scripting

Match words and fetch data in front of it in second column

Hi all, I have 2 files one file contain data like this in one column AST3 GSTY4 JST3 second file containign data like this in 2 columns AST3(PAXXX),GSTY4(PAXXY) it is used in diabetes KST4 it is used in blood... (6 Replies)
Discussion started by: manigrover
6 Replies

8. Shell Programming and Scripting

find common data

Hey guys, I have two files. file1 and file2. file1: a,1 b,2 c,343 d,343 e,4343 f,4544 file 2: a, d, e, Now i need to find the common data between these files from file1. i.e a,1 (8 Replies)
Discussion started by: jaituteja
8 Replies

9. Ubuntu

How to compare two columns and fetch the common data with additional column

Dear All, I am new to this forum and please ignore my little knowledge :p I have two types of data (a subset is given below) data version 1: 439798 2 1 451209 1 2 508696 2 1 555760 2 1 582757 1 2 582889 1 2 691827... (2 Replies)
Discussion started by: evoll
2 Replies

10. UNIX for Dummies Questions & Answers

how to find common words and take them out from two files

Hi, everyone, Let's say, we have xxx.txt A 1 2 3 4 5 C 1 2 3 4 5 E 1 2 3 4 5 yyy.txt A 1 2 3 4 5 B 1 2 3 4 5 C 1 2 3 4 5 D 1 2 3 4 5 E 1 2 3 4 5 First I match the first column I find intersection (A,C, E), then I want to take those lines with ACE out from yyy.txt, like A 1... (11 Replies)
Discussion started by: kaixinsjtu
11 Replies
Login or Register to Ask a Question