awk maintain case query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk maintain case query
# 1  
Old 11-09-2011
awk maintain case query

Code:
# Print list of word frequencies
{
$0 = tolower($0)
    for (i = 1; i <= NF; i++)
	   
		counter[$i]++	
}

END {
    
    for (word in counter)
        printf "%s\t%d\n",word, counter[word]		
}

I have this simple awk code from awk user guide to count the frequency of word. Now consider the following input to script:
hello HELLO Hello World

The output is:
hello 3
world 1

What if I want it to maintain the case of other words. i.e I want the output as
hello 3
World 1

(Note the case of World is retained). How do I do it. Please help

Last edited by pludi; 11-09-2011 at 06:06 PM..
# 2  
Old 11-09-2011
You could remove the tolower($0) but that would mess up your "hello"s...
# 3  
Old 11-09-2011
Code:
{     for (i = 1; i <= NF; i++)                 
         counter[($i~"^[Ww]orld$"):$i:tolower($i)]++     
}

# 4  
Old 11-09-2011
Need it to be generic

Hey Vgersh99,

Thank you for responding. How do I make it generic. I mean, the input is not always going to contain World.

The program will be run as follows:

gawk -f test.awk <input.txt

where, input.txt file can contain any text. Thanks
# 5  
Old 11-09-2011
Quote:
Originally Posted by ajacobs365
Hey Vgersh99,

Thank you for responding. How do I make it generic. I mean, the input is not always going to contain World.

The program will be run as follows:

gawk -f test.awk <input.txt

where, input.txt file can contain any text. Thanks
you'll need to know what words you'd like to keep in the original case. This can be made configurable and not be hard-wired to the code itself.
# 6  
Old 11-09-2011
vgersh99

Thank you for your patience. I need to maintain the case all the words other than the repetitive ones (with different cases), in which case it would take it as lower case.

For eg:
make Make MAKE the World A better place

Output:
make 3
the 1
World 1
A 1
better 1
place 1

You see, it uses only lowercase for same word and different cases (make in this case) and maintains the case of other words. When I use tolower to ignore the case while keeping the count, it keeps the count properly but unfortunately also makes other words lower caseSmilie
# 7  
Old 11-09-2011
Code:
nawk 'BEGIN{ARGV[ARGC++]=ARGV[1]} FNR==NR{for(i=1;i<=NF;i++) a[tolower($i)]++;next} {for(i=1;i<=NF;i++) (a[tolower($i)]>1)?c[tolower($i)]++:c[$i]++}END{for(i in c) print i,c[i]}' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to search case insensitive

Hello , Using the below scrip to search a string in a file , by case-insensitively Please assist on using the toupper() as getting error !. #!/usr/bin/ksh set -x curr_dir=`pwd` file_ctr=0 printf "\n Reviewing the output file from the directory: %s \n\n" $curr_dir ls -latr ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

awk special parse case

I have a special case that awk could be used but I do not have the skill. Trying to create a final output file (indel_parse.txt) that is created from using some information from each of the two files (attached). parse rules: The header is skipped FNR>1 1. 4 zeros after the NC_ (not... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Maintain line format using awk

Hello I have a file with the following format: ... text1 num num P # 2014--2-28-22---6 33.76--38.4173---21.9403----0.08-0.00--0.01--0.01--0.46----------0 text1 num num P text 2 num num S text 3 num num P ... (where "-"=space, "spaces" cannot... (4 Replies)
Discussion started by: phaethon
4 Replies

4. Shell Programming and Scripting

Need to maintain in- and output format with awk

Hi All, I have a data file (myfile.txt) as below: - A H C - A HHH F - AAA HH I The importan point is that the width between the columns are not fixed and the column seperator is space. I wish to change the value of 4th column using awk only when $3 = HH. I can... (4 Replies)
Discussion started by: angshuman
4 Replies

5. Shell Programming and Scripting

Making case insensitive in awk

here is a statement awk '/CREATE PROCEDURE/,/elimiter/' "$file1" > onlyproc1.sql which mean cut from create procedure to Delimiter or delimiter and paste it in onlyproc1.sql... my query is how to make this case insensitive.. that is i want the above code to work whther it is Delimiter or... (26 Replies)
Discussion started by: vivek d r
26 Replies

6. UNIX for Dummies Questions & Answers

menu + awk + while + case

the idea is to create script with menu and when option 1 or2 is pressed program should clear screan display info and get back to menu.... I managed some code but getting errors... #!/bin/bash choice1=ls -l|awk'{print $9 $1}' choice2= ls | wc -c choice3=exit while do clear echo "... (8 Replies)
Discussion started by: me.
8 Replies

7. Shell Programming and Scripting

case-insensitive search with AWK

Hi All, How we can perform case-insensitive search with AWK.:rolleyes: regards, Sam (11 Replies)
Discussion started by: sam25
11 Replies

8. Shell Programming and Scripting

what the awk do in this case

#!/bin/sh nowpwd=`pwd` cd $VAMPIRE_HOME configFile=$VAMPIRE_CFG/vampire.cfg inpuFileName=`awk -F\= '{ if ($1 == "PrepFilePos.InpFilePosDepo") { printf("%s",$2) exit 0 }}' < $configFile` I need to know what the awk doing here and what is the expected result here... (1 Reply)
Discussion started by: habuzahra
1 Replies

9. Shell Programming and Scripting

awk case-insensitive

can I tell awk to be case insensitive for one operation without setting the ignorecase value ? thanks, Steffen (7 Replies)
Discussion started by: forever_49ers
7 Replies

10. Shell Programming and Scripting

Case-insensitive serach with awk

Is there any way to do case insensitive search with awk for the below statement: month1=`awk '/month/' ${trgfile} | cut -d"=" -f2` the "month" could come as Month, mOnth,MONTH etc. in a file. Now I am looking for "month".... Thanks, AC (4 Replies)
Discussion started by: acheepi
4 Replies
Login or Register to Ask a Question