string having uniq code in the file

March 8, 2011 at 22:11:10
Specs: unix
hello i m new to unix so facing a problem here please help me out of here...

I am having a flat file having 3 main headings like Description|Ump_code|Speciality_code.
under it, it is having thousands of data which is like this...
it is my file.........
my input file is all_spec.txt

Addictive Diseases|25ADD|ADD
Addictive Diseases/Family Practice|25ADD|ADD/FP
Aerospace Medicine|1.041666667|AM
Aerospace Medicine/Family Practice|1.041666667|AM/FP
Allergy & Immunology|01AI|AI
Allergy & Immunology/Pulmonary Medicine|01AI|AI/PUL
Anatomic Pathology & Laboratory Medicine|17APL|APL
Cardiology|03C|C
Cardiology/Internal Medicine|03C|C/IM
Family Practice/|07FP|FP/PHP
Family Practice/ Retired|07FP|FP/Retired
Family Practice/ Surgery|07FP|FP/S


So i want a OUTPUT to be like this:

any of the specialty who should not contain sub-string and single form showing its uniq ump_code which got selected from the file,

Aerospace Medicine 1.041666667
Addictive Diseases 03C
Cardiology 03C
Family Practice 07FP
Allergy & Immunology 01A


See More: string having uniq code in the file

Report •

#1
March 9, 2011 at 06:52:36
If the first field does not contain a slash / print field 1 and field 2:

#!/bin/ksh

awk ' BEGIN { FS="|" }
{
if($1 !~ /\//)
    printf("%s %s\n", $1, $2)
} ' all_spec.txt > output.txt


Report •

#2
March 9, 2011 at 21:31:34
If i want to assign variable for fields, how can i please help me through it. and what if the first field contain /. and i want to run this file from another file let say aa.txt and get the output.

Thanks in advance


Report •

#3
March 9, 2011 at 22:15:17
I think you asked 3 questions.and the only one I think I understood is the one about the a.txt file - maybe? If aa.txt is like ali_spec.txt.

If you want more help, i require more information.


Report •

Related Solutions

#4
March 9, 2011 at 22:37:28
OK let me make you clear what exactly i want....

i want my output should contain the only the Description and its ump_code,
so as my file contain different description and ump_code so i want each and evry one's detail of both. like....

Aerospace Medicine 1.041666667
Addictive Diseases 03C
Cardiology 03C.

take eg like cardiology alone has its own ump_code and another line of cardiology with sub strings having different ump_code, so i dont want that ump_code. i want the exact field having no sub strings and its ump_code.
Cardiology|03C|C
Cardiology/Internal Medicine|03C|C/IM
so my output should show only the cardiology and its ump_code, not of other line containing cardiology with sub strings.
so want to assign a variable for every field so i can easily change my fields when ever i required as the Description content always changes or any other way you can make it.

hope you get the idea what i want.


Report •

#5
March 10, 2011 at 18:38:53
Sorry, but I still don't get it. It will take a smarter guy than me to help you out.

Report •

#6
March 10, 2011 at 21:47:59
its ok any ways thanks....

Report •

#7
March 10, 2011 at 22:03:17
ok lets me ce more simpler to make understand the question by changing it a little bit.....
I am having two flat files let say a.txt &b.txt.
a.txt file contain 3 columns say Speciality, Ump_code and speciality_code, like

SPECIALITY|UMP_CODE|SPECIALTY_CODE
Addictive Diseases|25ADD|ADD
Addictive Diseases/Family Practice|25ADD|ADD/FP
Aerospace Medicine|1.041666667|AM
Aerospace Medicine/Family Practice|1.041666667|AM/FP
Allergy & Immunology|01AI|AI
Allergy & Immunology/Pulmonary Medicine|01AI|AI/PUL
Anatomic Pathology & Laboratory Medicine|17APL|APL
Cardiology|03C|C
Cardiology/Internal Medicine|03C|C/IM
Family
Practice/|07FP|FP/PHP
Family Practice/ Retired|07FP|FP/Retired
Family Practice/ Surgery|07FP|FP/S

and file b.txt is having 1 column called Speciality
Speciality

Abdominal
ADDICTION MEDICINE
ALLERGY
Adolescent Medicine
Family Practice
General Practice

so i want an output where i have to map all the fields of b.txt file with the fields of a.txt and the output should consits of the speciality and the ump_code. and will save in another file.
OUTPUT:
SPECIALITY UMP_CODE
Aerospace Medicine 1.041666667
Addictive Diseases 03C
Cardiology 03C
Family Practice 07FP
Allergy & Immunology 01A

if any can help will be thank full to hin...
and i hope now u can get my alter question nails...


Report •

#8
March 12, 2011 at 21:38:42
You can probably make an educated guess, but just how are you suppose to tie the specialty code in the a.txt file to the description in the b.txt file?

To do what you want requires two columns in b.txt one column is the specialty code and the other is the description.


Report •

#9
March 15, 2011 at 01:55:16
what you said is clear to me, OK now just go through this again, i have written the code like this which get me only the mapped specialist but not the ump_code, so just help me out how to get the ump_code of the mapped fields..
awk -F"|"
' BEGIN{while (getline("all_spec.txt"a[$1]=1};
a[$1] == 1
{print $1}' sample.txt

my output is coming:
SPECIALITY
ADDICTION MEDICINE
ALLERGY
Family Practice
General Practice

ut i want both specialiest and ump_code of the fiel all_spec.txt


so now you can help i think so..


Report •

#10
March 15, 2011 at 13:36:55
First, your getline function call in your awk script is incorrect.

Second, I don't know what sample.txt looks like and I don't know if the all_spec.txt is the same. And I don't know if you want field 2 or field 3 printed. I choose field 2.

# UNTESTED!!
awk ' BEGIN { FS="|";
while ( getline < "all_spec.txt" > 0)
   a[$1]=$2
}
{
if($1 in a)
    printf("%s %s\n", $1, a[$1])
} ' sample.txt



Report •

#11
March 16, 2011 at 22:45:03
hey it work and i understand my earlier mistakes also.
thanks for your reply....

Report •

#12
March 22, 2011 at 22:43:19
one more question i have...
i m working on the same file and want to do some experiment on it.
Actually what i want to do now is that i want to add a 3rd column in my output file which will show the output
For that field which matched will show field matched side to it in another column and whose field not match will show field not matched. and now instead of showing the output in 3rd file i want to show the output in the 2nd file which consist of only one column and it will add 2 column ump_code and result column( which will show mapped or not mapped),
so give an rough idea over it please if you can.

Report •

Ask Question