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
# 8  
Old 08-14-2012
please provide the data for which output is not coming correctly

---------- Post updated at 10:18 AM ---------- Previous update was at 10:13 AM ----------

Also try this i made silly mistake in last one
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[y]=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

This User Gave Thanks to raj_saini20 For This Post:
# 9  
Old 08-14-2012
Hi Raj
Thanks for reply.

It seems to be working nice but becaue of one of my mistake I m facing one problem

I got below input file by putting comma in place of column and I didnt relise there are some commas within some column in a big file which make it diffiult to separate the results by comma.

Now I have to place underscore in place of column so that I can get more accurate reults.
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

Kindly guide to place underscore in place of column in a big file so that I can run above mentioned programm after that to get correct results.
# 10  
Old 08-14-2012
try this
Code:
awk -F"_" '
BEGIN{  OFS="_";
	f=0;
	i=1;
	while (getline < "7word")
		{
			a[$1]++;b[i++]=$1
		}
}
{
	for (j=1;j<=NF;j++)
		{
			n=split($j,sa," ");
			for(k1=1;k1<=n;k1++)
				{
					n2=split(sa[k1],sa2,",");
					
					for(k2=1;k2<=n2;k2++)
						{
							if(a[sa2[k2]])
								{
									a[sa2[k2]]=2;
									f=1
								}
						}
				};
				
			if(f==1)
				{
					
					$j=substr($j,1,match($j,",")-4);
					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

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