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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting toupper or tolower case of first letter of the line depending on another file
# 8  
Old 12-30-2011
Using your samples :
Code:
MacBookPro:kk klashxx$ cat gauche.txt 
Sized to 
Durable, midweight,
ultraviolet protection
quick flo
MacBookPro:kk klashxx$ cat DroiteInit.txt 
Dimensionné 
nylon durable 
Indice de Protection 
séchant flo
MacBookPro:kk klashxx$ awk 'NR==FNR{a=substr($0,1,1);b[FNR]=match(a,/[A-Z]/) ? 1 : 0;next}
{c=substr($0,1,1);c=b[FNR] ? toupper(c):tolower(c);print c""substr($0,2)}'  gauche.txt DroiteInit.txt
Dimensionné 
Nylon durable 
indice de Protection 
séchant flo

# 9  
Old 12-30-2011
And using Perl; the "bit-array" algorithm is similar to Klashxx's -

Code:
$
$ cat gauche.txt
Sized to
Durable, midweight,
ultraviolet protection
quick flo
$
$ cat DroiteInit.txt
Dimensionné
nylon durable
Indice de Protection
séchant flo
$
$
$ perl -lne 'if ($ARGV eq "gauche.txt") {/^(.)/ and push @x,(grep/$1/, (A..Z))?1:0}
             else {$x[$i++]==1?s/^(.)/\u$1/:s/^(.)/\l$1/; print}
            ' gauche.txt DroiteInit.txt
Dimensionné
Nylon durable
indice de Protection
séchant flo
$
$

tyler_durden
# 10  
Old 01-01-2012
Indeed, the match command returns 1 whatever the case....

---------- Post updated at 07:43 AM ---------- Previous update was at 07:40 AM ----------
Quote:
Originally Posted by durden_tyler
...sorry I need awk, thanks anyway
Quote:
Originally Posted by Klashxx
Using your samples :
Code:
MacBookPro:kk klashxx$ cat gauche.txt 
Sized to 
Durable, midweight,
ultraviolet protection
quick flo
MacBookPro:kk klashxx$ cat DroiteInit.txt 
Dimensionné 
nylon durable 
Indice de Protection 
séchant flo
MacBookPro:kk klashxx$ awk 'NR==FNR{a=substr($0,1,1);b[FNR]=match(a,/[A-Z]/) ? 1 : 0;next}
{c=substr($0,1,1);c=b[FNR] ? toupper(c):tolower(c);print c""substr($0,2)}'  gauche.txt DroiteInit.txt
Dimensionné 
Nylon durable 
indice de Protection 
séchant flo

My linux is crazy, I copy/paste your command, and get a different result:
Code:
----$ cat gauche.txt 
Sized to 
Durable, midweight,
ultraviolet protection
quick flo
----$ cat droiteInit.txt 
Dimensionné 
nylon durable 
Indice de Protection 
séchant flo
----$ awk 'NR==FNR{a=substr($0,1,1);b[FNR]=match(a,/[A-Z]/) ? 1 : 0;next}
{c=substr($0,1,1);c=b[FNR] ? toupper(c):tolower(c);print c""substr($0,2)}'  gauche.txt droiteInit.txt
Dimensionné 
Nylon durable 
Indice de Protection 
Séchant flo

??weird...any idea what is wrong?
# 11  
Old 01-01-2012
Try this... same code, a slight modification...
Code:
awk ' NR==FNR{ a=substr($0,1,1); b[FNR]=match(a,/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/) ? 1 : 0; next } 
{ c=substr($0,1,1); c=b[FNR] ? toupper(c):tolower(c); print c""substr($0,2) }' gauche.txt droiteInit.txt

--ahamed
# 12  
Old 01-01-2012
Quote:
Originally Posted by ahamed101
Code:
match(a,/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/)

--ahamed
yes this works well now:
Code:
---$ awk 'NR==FNR{a=substr($0,1,1);b[FNR]=match(a,/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/) ? 1 : 0;next}                                                                                                           
{c=substr($0,1,1);c=b[FNR] ? toupper(c):tolower(c);print c""substr($0,2)}'  gauchetest.txt droiteInittest.txt
Dimensionné 
Nylon durable 
indice de Protection 
séchant flo

but why? it should be the same, do you have an explanation?
# 13  
Old 01-01-2012
Quote:
Originally Posted by louisJ
yes this works well now:
but why? it should be the same, do you have an explanation?
I am trying to figure that out. That code change was just a hunch! I tried with awk, gawk, nawk etc it is all giving the same result.

--ahamed
# 14  
Old 01-01-2012
I don't get it...

how to write this as a awk script (and not a one liner), the following should be working, but it displays results from the first file (gauche.txt) instead of the right file (DroiteInit.txt) with the requiered changes...:

$ gawk -f ./awk_script gauche.txt droiteInit.txt
"awk_script" being:
Code:
NR==FNR 
{ 
    a=substr($0,1,1); 
    b[FNR]=match(a,/[AZERTYUIOPMLKJHGFDSQWXCVBN]/) ? 1 : 0; 
    next 
} 
{ 
    c=substr($0,1,1); 
    c=b[FNR] ? toupper(c):c=tolower(c);
    print c""substr($0,2) 
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

3. Shell Programming and Scripting

Upper case letter match

Hi, im able to search for string in a file (ex: grep -w "$a" input.txt). but i have to search for the uppercase of a string in a file where upper case of the file content matches something like below. where upper("$a")== converted to upper case string in (input.txt) can someone please provide... (5 Replies)
Discussion started by: p_satyambabu
5 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

Help with finding last line of file: if statement depending on that line.

Good morning, My first time actually posting in this forum, though I have used this forum to help with numerous projects. I am trying to figure out why my if statement does not work. I have a file where a line is inputted every 15 seconds. I want this if statement to check what the last line... (3 Replies)
Discussion started by: Shanrunt
3 Replies

6. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

7. Shell Programming and Scripting

HOWTO - change letter case of variable value

Hi I have e.g. VAR1=january and I need to change it into VAR1=January. How to change value of VAR1 variable to always set first character uppercase and other lowercase ? thx for help. (9 Replies)
Discussion started by: presul
9 Replies

8. Shell Programming and Scripting

Convert to upper case first letter of each word in column 2

Hi guys, I have a file separated by ",". I´m trying to change to upper case the first letter of each word in column 2 to establish a standard format on this column. I hope somebody could help me to complete the SED or AWK script below. The file looks like this: (Some lines in column 2... (16 Replies)
Discussion started by: cgkmal
16 Replies

9. UNIX for Dummies Questions & Answers

How to add a space after an Upper case letter

Hi I have two questions. 1. how to convert "EverythingIsFine" to "Everything Is Fine" in a txt file. 2. how to convert everything to upper case letter and reverse, I hope there is a general purpose script for this. (1 Reply)
Discussion started by: onthetopo
1 Replies

10. Shell Programming and Scripting

Covert case of first letter only

Using sed or tr, how do I change "fred" into "Fred" or "fReD" into "Fred" (1 Reply)
Discussion started by: tisons
1 Replies
Login or Register to Ask a Question