Visit Our UNIX and Linux User Community


Awk Help - toupper/tolower


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk Help - toupper/tolower
# 1  
Old 06-08-2012
Awk Help - toupper/tolower

Hi,

I am learning awk and faced few queries. Kindly suggest on the same. Where it is wrong.

Code:
$ 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
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

$ awk '{if (tolower($1) ~ /A/) print $0}' inv

$ awk '{if (tolower($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
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

# 2  
Old 06-08-2012
toupper($1) ~ /A/ => If the first field (converted to uppercase) contains 'A' ""anywhere"" in the string.

Try this instead: toupper($1) ~ /^A/ to filter out the first field ""starting"" with 'A'

And, I'm not able to understand the second requirement. Do you want to search in first field if second character is 'a'?

Last edited by balajesuri; 06-08-2012 at 09:57 AM..
# 3  
Old 06-08-2012
Quote:
Originally Posted by balajesuri
toupper($1) ~ /A/ => If the first field (converted to uppercase) contains 'A' ""anywhere"" in the string.

Try this instead: toupper($1) ~ /^A/ to filter out the first field ""starting"" with 'A'

And, I'm not able to understand the second requirement. Do you want to search in first field if the second character is 'a'?
My concern is that in first case, it should pick "A" only not "a" irrespective of the position.

Code:
$ 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
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

In second case, it should not pic "A" but "a"

Code:
$ awk '{if (tolower($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
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

# 4  
Old 06-08-2012
For first case, did you try this? toupper($1) ~ /^A/

For second case,
Code:
# awk 'toupper($1) !~ /^A/ && tolower($1) ~ /a/ {print}' inv
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

# 5  
Old 06-08-2012
toupper, tolower converts the argument to upper or lowercase...
So what was returned is what is expected...
# 6  
Old 06-08-2012
Quote:
Originally Posted by vbe
toupper, tolower converts the argument to upper or lowercase...
So what was returned is what is expected...
How it has returned what is expected. Using CYGWIN_NT-5.1

Code:
awk '{if (toupper($1) ~ /A/) print $0}' inv

Code:
Input file :

$ cat inv

Jan  13  25  15 115
Feb  15  32  24 226
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Jun  31  42  75 492
Jul  24  34  67 436
Aug  15  34  47 316
Sep  13  55  37 277
Oct  29  54  68 525
Nov  20  87  82 577
Dec  17  35  61 401

Jan  21  36  64 620
Feb  26  58  80 652
Mar  24  75  70 495
Apr  21  70  74 514

Should return as under :

Code:
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

rest are unwanted like :
Code:
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495


Last edited by Scrutinizer; 06-08-2012 at 10:48 AM.. Reason: additional code tags
# 7  
Old 06-08-2012
Then don't use toupper:
Code:
awk '$1~/^A/' inv

 

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

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

6. Shell Programming and Scripting

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.nawk 'NR==FNR{a=$4" "$5}NR>FNR{print NF?$0:a"\n";if(/^cn:/) x=toupper($0)}' FS="" id_list.txt attributes.txt > out In the above script, id_list... (9 Replies)
Discussion started by: Samingla
9 Replies

7. Programming

tolower (static pointer + malloc + realloc)

N00B here. This function would be easier using a char pointer along with free. But I wish to learn how to use char static pointers (they do not require free, right ?). How do I erase the content of a static pointer ? Terminating the string works but the static pointer's content is not being... (4 Replies)
Discussion started by: limmer
4 Replies

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

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

How to apply a "tolower" AWK to a few parts of a document

Hi people, i have a nice problem to solve.. in an text page i must change all the "*.php" occourences to the respective lowercase.. Example: ... <tr><td> <form action="outputEstrazione.php" method="get"> <table cellspacing='0,5' bgcolor='#000000'><tr><td> <font size='2'... (5 Replies)
Discussion started by: marconet85
5 Replies

Featured Tech Videos