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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match words and fetch data in front of it in second column
# 1  
Old 08-16-2012
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

Code:
AST3
GSTY4
JST3


second file containign data like this in 2 columns


Code:
AST3(PAXXX),GSTY4(PAXXY)              it is used in diabetes 
KST4                                                     it is used in blood pressure
JST3                                                    it is never applied in oedema

I have match column of first file with second file and if similarty of symbol which are made up of capital alphabets is there then it shuld copy the sentence in front of it

The Expected out put is

Code:
AST3(PAXXX)                               it is used in diabetes
GSTY4(PAXXY)                            it is used in diabetes 
JST3                                            it is never applied in oedema


If it possible to remove the bracket then it will be good in expected output


Code:
AST3                              it is used in diabetes
GSTY4                         it is used in diabetes 
 JST3                                            it is never applied in oedema

---------- Post updated at 08:25 AM ---------- Previous update was at 02:37 AM ----------

Quote:
Originally Posted by manigrover
Hi all,

I have 2 files

one file contain data like this in one column

Code:
AST3
GSTY4
JST3


second file containing data like this in 2 columns


Code:
AST3(PAXXX),GSTY4(PAXXY)              it is used in diabetes 
KST4                                                     it is used in blood pressure
JST3                                                    it is never applied in oedema

I have match column of first file with second file and if similarty of symbol which are made up of capital alphabets is there then it shuld copy the sentence in front of it

If it possible to remove the bracket then it will be good in expected output


Code:
AST3                              it is used in diabetes
GSTY4                         it is used in diabetes 
 JST3                                            it is never applied in oedema


Last edited by manigrover; 08-16-2012 at 10:24 AM..
# 2  
Old 08-16-2012
Code:
sed 's/\(..*\)//' file2
or
sed 's/(..*)//' file2


Last edited by bobbygsk; 08-16-2012 at 12:02 PM..
# 3  
Old 08-16-2012
If you don't mind getting rid of all of the whitespace between the 1st and 2nd fields in your second file when writing the output:
Code:
awk 'FNR==NR {wanted[NR] = $1; next}
 {      nf=split($1, f1, ",")
        for (i=1; i<=nf; i++) {
                sub("[(][^(]*[)]", "", f1[i])
                for (j in wanted) if (wanted[j] == f1[i]) {
                        $1=f1[i]
                        print
                }
        }
}' first_file second_file

produces
Code:
AST3 it is used in diabetes
GSTY4 it is used in diabetes
JST3 it is never applied in oedema

when given your two sample input files.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-04-2012
Request to check

Hi

Thanks for reply.

I checked regarding white space but there is space between every two words mentiioned in the 2 files and output is just same as inpu of second file there is no change in output and second input file

Kindly guide
# 5  
Old 09-04-2012
Quote:
Originally Posted by manigrover
Hi

Thanks for reply.

I checked regarding white space but there is space between every two words mentiioned in the 2 files and output is just same as inpu of second file there is no change in output and second input file

Kindly guide
In the second file you showed in the 1st message in this thread, you have somewhere between 10 and 60 spaces between columns. In the expected output, you showed lots of spaces between the 1st and 2nd columns (and a space at the front of the last line). The script I provided never puts a space at the start of an output line and always puts a single space between the drug name and the usage note.

If it is important for you to keep the same spacing between columns in the output that was present in the input, the script will be a more complex (and I believe the output would be harder to read).
# 6  
Old 09-09-2012
Request to check

Hi


I have checked actually in the second file I don't tthink there is problem related to spacing.

In actual file there are just 2 column of excel sheet in second file

And, In output also I do not want lots of spacing I just want output should be like second file. And, there should n't be any extra spacing.

Kindly check it and let me know any solution if possible
# 7  
Old 09-10-2012
The output produced by the script I supplied in message #3 in this thread is:
Code:
AST3 it is used in diabetes
GSTY4 it is used in diabetes
JST3 it is never applied in oedema

The output you said you want in the updated first message in this thread is:
Code:
AST3                              it is used in diabetes
GSTY4                         it is used in diabetes 
 JST3                                            it is never applied in oedema

I assume that you can see that there is a space before JST3 in the third line and there are lots of spaces between the end of the first column and theitat the start of the second field in each of the three output lines.

Please tell me if the solution I provided earlier is sufficient to solve your problem. If it is not; please explain where the space at the start of line three of your output came from. And, if more than one space (or tab) is needed between columns one and two in the output, explain how we can determine how many spaces should be there. (Especially, explain how the number of spaces between fields is supposed to be determined when input lines are split and when the parenthetical elements are removed.)
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

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 F2 F3 YUH SUH second fiel several columns like excel sheet (8-9) but my file in text F2 fgf gfhjhjghjghj dhgfhgfh 234324 F3 ... (6 Replies)
Discussion started by: Priyanka Chopra
6 Replies

4. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

5. Shell Programming and Scripting

Match same file column data

File A B07 U51C 4434 L662C 4412 B07 L64U 612 L651B 4434 B07 L11C 4434 R151B 4434 B05 L12Z 612 L51B 4434 B01 651Z 612 L651C 4434 B04 A51Z 612 L51A 4434 L07 B08D 4434 B1B 4434 B07 RU8D 4434 L51A 4434 B07 L58D 4434 B51C 4434 B07 LA8D 4434 L4B 4434 Now i want File B Output B07... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

Match the word or words and fetch the entries

Hi all, I have 7 words Now I have 1 file which contain data in large number of rows and columns and 6th column contain any of these words or may be more than one words among above 7 words: I want script should search for the above mentioned 7 words in the 6th column ... (9 Replies)
Discussion started by: manigrover
9 Replies

7. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

8. 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

9. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

10. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies
Login or Register to Ask a Question