Match word and put data before it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match word and put data before it
# 1  
Old 08-13-2012
Match word and put data before it

Hi all I have 7 words ina file called "lookupfile"

CAD
CD
HT
RA
T1D
T2D
BD
in other file I have data like this in which columns are seaprated by comma but the names among above seven names are in one column menas comma between these seven words doesnt mean that they are separated by column but other commas dentoe separation by column

These word are present in each n+1 column where n=2

Code:
FHIT ,Adenosine Monotungstate,Not Available CAD,HT,HT,T2D,Ado-P-Ch2-P-Ps-Ado,Not Available CAD,HT,HT,T2D
CS ,Trifluoroacetonyl Coenzyme A,Not Available CAD,CAD,T1D,Alpha-Fluoro-Carboxymethyldethia Coenzyme a Complex,Not Available CAD,CAD,T1D,Nitromethyldethia Coenzyme A,Not Available CAD,CAD,T1D

I need to separate entries with these words anf get 7 files

for each entry

I thied follwing programm but it didnt wrk properly

Code:
awk '
BEGIN{
	f=0;
	i=1;
	while (getline < "7wordfile")
		{
			a[$1]++;b[i++]=$1
		}
}
{
	for (j=1;j<=NF;j++)
		{
			n=split($j,c,",");
			for(k=1;k<=n;k++)
				{
					if(a[c[k]])
						{
							a[c[k]]=2;
							f=1
						}
				};
			if(f==1)
				{
					$j=x;
					f=0
				}
		};
	for(k=1;k<i;k++)
		{
			if(a[b[k]]==2)
				{
					a[b[k]]=1;
					print > b[k]
				}
		}

}' inputfile
# 2  
Old 08-13-2012
Code:
awk -F"," '
BEGIN{  OFS=",";
	f=0;
	i=1;
	while (getline < "7wordfile")
		{
			a[$1]++;b[i++]=$1
		}
}
{
	for (j=1;j<=NF;j++)
		{
			
			if(a[$j])
				{
					a[$j]=2;
					f=1
				};
				
			if(f==1)
				{
					$j=x;
					f=0
				}
		};
	for(k=1;k<i;k++)
		{
			if(a[b[k]]==2)
				{
					a[b[k]]=1;
					print > b[k]
				}
		}
}' input

# 3  
Old 08-13-2012
Request to check

Hi Raj


Thanks for reply.

Using the above mentioned programm results are not accurate even less better than the previous one as

for example

in T2D file

not all entries of T2D are pesent

using new programm it shows only 9 entries in T2D file with CAD,T2D also mentioned in front which I dont want
usin previous program I got 33 entries in T2D file from my big file and these were actually correlate with input I checked some of these 33 which are absent in the output using new program

The previous programm was doing good but not following the rule of n+2 therfore not showing all entries in a row properly
# 4  
Old 08-13-2012
please provide the sample output that should be present in the 7files.

what to do with CAD in this data "Not Available CAD",
# 5  
Old 08-13-2012
Request to check

Hi Thanks for reply

for eg

expected output for T2D according to given DATA
Code:
FHIT ,Adenosine Monotungstate,Not Available,do-P-Ch2-P-Ps-Ado,Not Available

for T1D

Code:
CS ,Trifluoroacetonyl Coenzyme A,Not Available,Alpha-Fluoro-Carboxymethyldethia Coenzyme a Complex,Not Available,Nitromethyldethia Coenzyme A,Not Available

for CAD
Code:
FHIT ,Adenosine Monotungstate,Not Available,Ado-P-Ch2-P-Ps-Ado,Not Available

CS ,Trifluoroacetonyl Coenzyme A,Not Available,Alpha-Fluoro-Carboxymethyldethia Coenzyme a Complex,Not Available,Nitromethyldethia Coenzyme A,Not Available

In the same way for HT and others.
# 6  
Old 08-13-2012
Try this if any discrepancy with required output let me know
Code:
awk -F"," '
BEGIN{  OFS=",";
	f=0;
	i=1;
	while (getline < "7wordfile")
		{
			a[$1]++;b[i++]=$1
		}
}
{
	for (j=1;j<=NF;j++)
		{
			x=substr($j,length($j)-2,3);
			y=substr($j,length($j)-1,2);
			if(a[x])
				{
					a[x]=2;
					$j=substr($j,1,length($j)-4)
				}
			else
				{
					if(a[y])
						{
							a[x]=2;
							$j=substr($j,1,length($j)-3)
						}
				};
			if(a[$j])
				{
					a[$j]=2;
					f=1
				};
				
			if(f==1)
				{
					$j=z;
					f=0
				}
		};
	for(k=1;k<i;k++)
		{
			if(a[b[k]]==2)
				{
					a[b[k]]=1;
					for(l=1;l<=NF;l++) 
						{
							if(length($l))
								{
									printf $l"," > b[k]
								}
					
						};
					printf "\n"  > b[k]
				}
		}
}' input

# 7  
Old 08-13-2012
Request to check

Hi

Tkanlyouvery much for your patience.

But still it doesn't give me the whole list

for eg in RA file

there re only 19 entries while from the first programm which is written here
its giving correct 25 entries

so there is some error

But thank youvery much for urhelp again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

3. Shell Programming and Scripting

Problem to match a path in a file and put it into a variable

Hello,:p I made a script which do a backup on remote servers with a rsync command. I have a config.cfg with the IPs and the paths where it will copy the directory. The problem is that it doesn't match the paths, So, here my script and its output with the debug : #!/bin/bash # PATHS... (7 Replies)
Discussion started by: Arnaudh78
7 Replies

4. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

5. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

6. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

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

8. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

9. Shell Programming and Scripting

Put raw data to column data

Dear all, I want below data to make it in column format.so i will see the data like this cdrID teleServiceCode chargedPartyNumber ... ... ... ... "egmailcom0w10ggzx00" 'sMS (5)' "716323770" "m17ifi5z30w0z6o7200" 'sMS (5)' ... (7 Replies)
Discussion started by: Nayanajith
7 Replies

10. Shell Programming and Scripting

put each word in new line - sed or tr

Hello ! I have a result of ls command in a file: file1 file2 file3.out file4.pdf file5 they all are separated by space. I need to put them on a separate line example: file1 file2 file3.out file4.pdf fil35 i tried sed 's/ /\n/g' inputfile > outputfile but did not help (3 Replies)
Discussion started by: hemangjani
3 Replies
Login or Register to Ask a Question