issue with toupper in nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issue with toupper in nawk
# 1  
Old 04-21-2011
issue with toupper in nawk

Hi All,

I am seeing an issue while using toupper in nawk. Below is the code that I am using.Toupper method is not working as expected in this case.
Code:
nawk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{print NF?$0:a[x]"\n";if(/^cn:/) x=toupper($0)}' FS="[, ]" id_list.txt attributes.txt > out

In the above script, id_list file contains list of cn values and their corresponding id. All cn values in this file are in capitol letters. Attributes file contains list of of parameters along with cn parameter. Some cn values in this file are in small letters.
The script should get cn value from id_list, search attributes file and if matching cn is found add an entry with the corresponding id value from id_list file. Script is working fine for cn values in capitol lettors, it is not wirking for cn values in small letters. Am I missing any thing here? Is there any other way to accomplish this. Your help is highly appriciated.
Below are the input files and expected output file.

Code:
id_list
cn: abc, id: 111
cn: DEF, id: 222
cn: ghi, id: 333
cn: JKL, id: 444

Code:
Attributes file
gn: abc
cn: abc
objclass: test
uid: abc
 
gn: DEF
cn: DEF
objclass: test
uid: DEF
 
gn: EGH
cn: EFG
objclass: test
uid: EFG
 
gn: ghi
cn: ghi
objclass: test
uid: ghi
 
gn: JKL
cn: JKL
objclass: test
uid: JKL

Code:
output
 
gn: abc
cn: abc
objclass: test
uid: abc
id: 111
 
gn: DEF
cn: DEF
objclass: test
uid: DEF
id: 222
 
gn: EGH
cn: EFG
objclass: test
uid: EFG
 
gn: ghi
cn: ghi
objclass: test
uid: ghi
id: 333
 
gn: JKL
cn: JKL
objclass: test
uid: JKL
id: 444

Thanks in advance,
Sam
# 2  
Old 04-22-2011
Give this a try:
Code:
nawk -F"[ ,]" 'NR==FNR{a[toupper($1$2)]=$4 OFS $5; next}
{idx=toupper($1$2)}
idx in a {u=a[idx]}
u && /uid:/{$0=$0 RS u; u=""}
1' id_list.txt  attributes.txt > out

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-22-2011
I'm not familiar with nawk, but shouldn't the toupper be done before the print?
Code:
if(/^cn:/) x=toupper($0); print NF?$0:a[x]"\n"

Also, I just noticed this:
Quote:
id_list file contains list of cn values and their corresponding id. All cn values in this file are in capitol letters
but in your example:
Code:
id_list
cn: abc, id: 111
cn: DEF, id: 222
cn: ghi, id: 333
cn: JKL, id: 444

Which is correct?

Also "cn:" in id_list must be capitals as well.

Last edited by kato; 04-22-2011 at 11:14 AM..
# 4  
Old 04-22-2011
Kota,

Thanks for the reply. Yes all cn values in id_list file are in capitol letters. Mistake from my end. id_list file will be like this.

Code:
cn: ABC, id: 111
cn: DEF, id: 222
cn: GHI, id: 333
cn: JKL, id: 444

Thanks,
Sam

---------- Post updated at 11:34 AM ---------- Previous update was at 11:07 AM ----------

Quote:
Originally Posted by Franklin52
Give this a try:
Code:
nawk -F"[ ,]" 'NR==FNR{a[toupper($1$2)]=$4 OFS $5; next}
{idx=toupper($1$2)}
idx in a {u=a[idx]}
u && /uid:/{$0=$0 RS u; u=""}
1' id_list.txt  attributes.txt > out

Hi Franklin,

Thanks for the update. Your code is working as expected. I have one more question. How can we add a contstant string xxx: mno in attributes file if the cn value is found in attributes file using the same code?


Thanks in advance,
Sam

Last edited by Samingla; 04-22-2011 at 01:38 PM.. Reason: duplicate
# 5  
Old 04-22-2011
Quote:
Originally Posted by Samingla
Hi Franklin,

Thanks for the update. Your code is working as expected. I have one more question. How can we add a contstant string xxx: mno in attributes file if the cn value is found in attributes file using the same code?


Thanks in advance,
Sam
Sorry, I don't think I understand the question, can you explain what you mean and give an example?
# 6  
Old 04-22-2011
Hi Franklin,
Along with the id value from id_list file, how can we add a constant string (Example xxx: mno) in attributes file using the same code?

Thanks,
Sam

---------- Post updated at 11:54 AM ---------- Previous update was at 11:51 AM ----------

Frankin,
The out put should be some thing like this
Code:
gn: abc
cn: abc
objclass: test
uid: abc
id: 111
xxx: mno
 
gn: DEF
cn: DEF
objclass: test
uid: DEF
id: 222
xxx: mno
 
gn: EGH
cn: EFG
objclass: test
uid: EFG
 
gn: ghi
cn: ghi
objclass: test
uid: ghi
id: 333
xxx: mno
 
gn: JKL
cn: JKL
objclass: test
uid: JKL
id: 444
xxx: mno

Thanks,
Sam
# 7  
Old 04-22-2011
Something like this?
Code:
nawk -F"[ ,]" 'NR==FNR{a[toupper($1$2)]=$4 OFS $5; next}
{idx=toupper($1$2)}
idx in a {u=a[idx]}
u && /uid:/{$0=$0 RS u RS "xxx: mno"; u=""}
1' id_list.txt  attributes.txt > out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Substitution Issue with nawk

Hi, I'm trying to reformat some badly formatted XML that I've extracted from Oracle clob columns using the following nawk command: nawk '{gsub(/</,/>\n/); print}' test.raw > test.xml the substitution executes fine, but instead of subbing < with > followed by newline, it subs the < with a... (3 Replies)
Discussion started by: sffuji
3 Replies

3. Shell Programming and Scripting

issue with nawk while using variable assignment

#ifconfig -a | nawk '/1.1.1.1/{print}' inet 1.1.1.1 netmask xxxxxxxxx broadcast 0.0.0.0 If i assign the ip to a variable and search for the variable nothing gets printed!! # ifconfig -a | nawk -v ip=1.1.1.1 '/ip/{print}' I am not able to understand why this is happening! (6 Replies)
Discussion started by: chidori
6 Replies

4. UNIX for Dummies Questions & Answers

Awk Help - toupper/tolower

Hi, I am learning awk and faced few queries. Kindly suggest on the same. Where it is wrong. $ awk '{if (toupper($1) ~ /a/) print $0}' inv $ awk '{if (toupper($1) ~ /A/) print $0}' inv -- Why this output Jan 13 25 15 115 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208... (6 Replies)
Discussion started by: vanand420
6 Replies

5. Shell Programming and Scripting

nawk issue

Hi, Below work fine, whenever any character puts, however if we use "(", it's not working. Working =============== echo 'Sau(rabh is Nice' | nawk -v a="Saurabh" '{print substr($1,1,match($1, "u"))}' Not working ==================== echo 'Sau(rabh is Nice' | nawk -v a="Saurabh" '{print... (3 Replies)
Discussion started by: sbaisakh
3 Replies

6. Shell Programming and Scripting

toupper or tolower case of first letter of the line depending on another file

Hi I would like to read if the first letter of a line in a first file (gauche.txt) is uppercase or lowercase, and change consequently the first letter of the corresponding line in the second file (droiteInit.txt). I have done this but it won't work (I launch this using gawk -f... (16 Replies)
Discussion started by: louisJ
16 Replies

7. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

8. Shell Programming and Scripting

grep / nawk issue

I have a report which contains the following: Count Value % 47 69.12 18 26.47 3 4.41 I want to grep the total on the bottom brackets and store in a variable. However this may have a different figure everyday. To read the i do: ... (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

9. Shell Programming and Scripting

Handling multiple fields of a database file for toupper() function in awk

hello everyone.... script is: To convert the contents of a database file into uppercase my code is: printf "%s\n" , $2 | awk '{print toupper($2)}' emp.lst i m able to do only for one field.....didn't get any sources for handling multiple fields. please suggest me for multiple... (1 Reply)
Discussion started by: Priyanka Bhati
1 Replies

10. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies
Login or Register to Ask a Question