awk record matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk record matching
# 1  
Old 02-24-2017
awk record matching

ok. so i have a list of country names which have been abbreviated. we'll call this list A

i have another list that which contains the what country each abbreviated name means. we'll call this list B.


so example of the content of list B:

Code:
#delimited by tabs
#ABBR COUNTRY     COUNTRY ABBR
so      Somalia ST. KITTS AND NEVIS     KN
sr      Suriname        ST. LUCIA       LC
st      Sao Tome And Principe   ST. PIERRE AND MIQUELON PM
su      Soviet Union    ST. VINCENT & THE GRENADINES    VC
sv      El Salvador     SUDAN   SD
sy      Syrian Arab Republic    SURINAME        SR
sz      Swaziland       SVALBARD AND JAN MAYEN  SJ
tc      Turks And Caicos Islands        SWAZILAND       SZ
td      Chad    SWEDEN  SE
tf      French Southern Territories     SWITZERLAND     CH
tg      Togo    SYRIAN ARAB REPUBLIC    SY
th      Thailand        TAIWAN  TW
tj      Tajikistan      TAJIKISTAN      TJ
tk      Tokelau TANZANIA, UNITED REPUBLIC OF    TZ
ti      East Timor (new code)   THAILAND        TH
tm      Turkmenistan    TOGO    TG
tn      Tunisia TOKELAU TK
to      Tonga   TONGA   TO
tp      East Timor (old code)   TRINIDAD AND TOBAGO     TT
tr      Turkey  TUNISIA TN
tt      Trinidad And Tobago     TURKEY  TR
tv      Tuvalu  TURKMENISTAN    TM
tw      Taiwan  TURKS AND CAICOS ISLANDS        TC
tz      Tanzania, United Republic Of    TUVALU  TV
ua      Ukraine UGANDA  UG
ug      Uganda  UKRAINE UA
uk      United Kingdom  UNITED A

how can i get awk to tell me the full country name if i supply the country's abbreviation? also note, some abbreviated countries (left column) are in lowercase, and others are in CAPs (right column).

The above list of countries is stored in a variable. so i intend to do something like this:

Code:
#!/bin/sh
SuppName=${1}
echo "${ListBContent}" | awk -v SupName="${SupName}" '(($1 ~ SupName) || ($3 ~ SupName)) {print $0}'

My question is, is there a more efficient, better way to do this? i
# 2  
Old 02-24-2017
You probably had to read the file to begin with, in order to place it in a variable. So,
Code:
awk '{ awk program} ' filename

is probably the most efficient form.

The use of pattern matching is okay, except that $3 in your example is uppercase in the file and not in the code.

I'm not sure what else you are after. If your list is huge and the country abbreviation is always lowercase and in column 1 then:

to look for country==aa with /^aa/ is more efficient assuming all available country codes exist in the input file at column 1. Or $1="aa" is also fast since there are only two characters in the search pattern.

I would just use grep and skip awk
Code:
arr=$( grep -ie '(^aa| aa)'  filename)
[ $? -eq 0 ] && echo "${arr[1]} ${arr[2]}"


Last edited by jim mcnamara; 02-24-2017 at 07:38 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-24-2017
Try:
Code:
awk '
  NR==FNR {
    n=split($3,F," ")
    c=x
    for(i=1; i<=n; i++) c=c toupper(substr(F[i],1,1)) tolower(substr(F[i],2)) " "
    A[$1]=$2
    A[tolower($4)]=c
    next
  }
  {
    print $1, A[tolower($1)]
  }
' FS='\t' OFS='\t' listB listA


Last edited by Scrutinizer; 02-24-2017 at 11:33 PM..
This User Gave Thanks to Scrutinizer 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

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk print matching records and occurences of each record

Hi all , I have two files : dblp.xml with dblp records and itu1.txt with faculty members records. I need to find out how many dblp records are related to the faculty members. More specific: I need to find out which names from itu1.txt are a match in dblp. xml file , print them and show how many... (4 Replies)
Discussion started by: iori
4 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. UNIX for Dummies Questions & Answers

awk: record too long

Hi All , I am getting record too long for the below command . nawk -F\" '{a=a" "$2} END{for(i in a) print i,a }' test|sort|awk '{for(i=1;i<=NF;i++) t=t"\t"$i;if(NF>max)max=NF} END{for(i=1;i<=max;i++)print t }' File test has 850 records ... Please help.. (2 Replies)
Discussion started by: saj
2 Replies

5. Shell Programming and Scripting

awk: matching and not matching

Hello all, simple matching and if not match problem that i can't figure out. file1 hostname: 30 10 * * * /home/toto/start PROD instance_name1 -p 00 9 * * * /home/toto/start PROD instance_name2 -p 15 8 * * * /home/toto/start PROD instance_name3 -p hostname2: 00 8 * * *... (5 Replies)
Discussion started by: maverick72
5 Replies

6. Shell Programming and Scripting

selecting record by matching in two columns values

Hi Guys ! i want to search a record in file by matching two values in a record in two different columns suppose i have 3 columns and i want to select all those values from col1 for which in col3 has a specific value e.g select all "john" from column1 where column 3 has a value of "20" ... (9 Replies)
Discussion started by: ourned
9 Replies

7. UNIX for Dummies Questions & Answers

Record splitting with AWK

Hi all ! I need your help as quick as possible. My input file like this: bạc těnh ( 薄情) 1 . 薄情な.2. 夫婦或いは男女の不貞を指す。 bách (百,迫)1.100ドソ. tr a m b a c ともいう. 2.柏(カヽしわ)・ 3.圧迫する.4.差し迫った, My propose is take the value in the firt bracket. I used the command like : ...if (index(... (6 Replies)
Discussion started by: maixu134
6 Replies

8. Shell Programming and Scripting

select a record from one file matching from second file using awk

I need help :) I have two input files and I'd like to generate a report based on the two. filea: hostname,account1,password ,account2,password hostname,account1,password hostname,account1,password ,account1,password ,account2,password repeating hostnames are blank fileb: hosta... (7 Replies)
Discussion started by: synmag
7 Replies

9. Shell Programming and Scripting

AWK - if last line/record do something

Hello: I am trying to perform a certain action if the current record is the last line of the input file. But I am unable to figure out how to determine the last line of a file in awk. I need to do something like this: awk '{ if (lastline == NR) Do Something}' myfile.txt I have tried the... (3 Replies)
Discussion started by: PacificWonder
3 Replies

10. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies
Login or Register to Ask a Question