# Convert text to lower case except the strings within single quotes

**URL:** <https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308>\
**Category:** Shell Programming and Scripting\
**Created:** [April 27, 2017, 6:02am UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308 "2017-04-27T06:02:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_K](https://community.unix.com/letter_avatar/john_k/32/6_449c533f586939b6e5f246632a46421f.png) [@John\_K](https://community.unix.com/u/John_K)\
**Post date:** [April 27, 2017, 6:02am UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/1 "2017-04-27T06:02:02Z")

</div>

Shell : bash that comes with RHEL 6.7

I have SQL scripts like below. I want to convert all the text in these files to lower case except the strings enclosed within single quotes .  
Any idea how I can achieve this ?

Sample text:

```nohighlight
$ cat sample.txt
SELECT A.LOGIN_ID,A.password,NVL(A.TEMP_PASS_EXPIRY_DT,SYSTIMESTAMP),NVL(UPPER(A.STATUS), ' ')
  INTO V_LOGIN_ID, V_PASSWORD , V_TEMP_PASS_EXPIRY_DT,V_STATUS
  FROM W_DEALER_MASTER A, W_DEALER_USER_MAP B
  WHERE A.LOGIN_ID = I_LOGINID AND B.LOGIN_ID=A.LOGIN_ID AND B.ROLE_ID in ('sysAdmSupervisorR2','sysAdmSalesRepR2');

        IF V_PASSWORD = v_entered_password AND V_STATUS = 'ACTIVE' THEN
                        O_STATUS := 'SUCCESS';
                        O_MESSAGE := null;
                ELSE
                    O_STATUS := 'FAIL';

        

```

Expected output:

```nohighlight
select a.login_id,a.password,nvl(a.temp_pass_expiry_dt,systimestamp),nvl(upper(a.status), ' ')
  into v_login_id, v_password , v_temp_pass_expiry_dt,v_status
  from w_dealer_master a, w_dealer_user_map b
  where a.login_id = i_loginid and b.login_id=a.login_id and b.role_id in ('sysAdmSupervisorR2','sysAdmSalesRepR2');
    
    if v_password = v_entered_password and v_status = 'ACTIVE' then 
            o_status := 'SUCCESS';
            o_message := null;
        else 
            o_status := 'FAIL';

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 27, 2017, 6:11am UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/2 "2017-04-27T06:11:16Z")

</div>

Any attempts / ideas / thoughts from your side?

---

<div class="post-metadata">

**Author:** ![AbelLuis](https://community.unix.com/user_avatar/community.unix.com/abelluis/32/15523_2.png) [@AbelLuis](https://community.unix.com/u/AbelLuis)\
**Post date:** [April 29, 2017, 9:01pm UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/3 "2017-04-29T21:01:26Z")

</div>

Hi, John K,  
you may try this:

```nohighlight
awk '{ 
           for (i=1; i<=NF; i++ )
                   if ( substr($i, 1, 1) == "\x27" && substr($i, length($i), 1)=="\x27") 
                           printf("%s ", $i);
                   else printf("%s ", tolower($i));
                  
                   printf("\n");

        }' sample.txt

```

Greetings!

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [April 30, 2017, 9:21am UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/4 "2017-04-30T09:21:50Z")

</div>

Hi, AbelLuis.

> [@abelluis](#):
>
> Hi, John K,  
> you may try this:
> 
> ```plaintext
> awk '{ 
> for (i=1; i<=NF; i++ )
> if ( substr($i, 1, 1) == "\x27" && substr($i, length($i), 1)=="\x27") 
> printf("%s ", $i);
> else printf("%s ", tolower($i));
>    
> printf("\n");
> 
> }' sample.txt
> 
> ```
> 
> Greetings!

Unless there is something additional here (or that I missed), this appears to fail if the quoted string is not bounded by whitespace ... cheers, drl

Results:

```plaintext
select a.login_id,a.password,nvl(a.temp_pass_expiry_dt,systimestamp),nvl(upper(a.status), ' ') 
into v_login_id, v_password , v_temp_pass_expiry_dt,v_status 
from w_dealer_master a, w_dealer_user_map b 
where a.login_id = i_loginid and b.login_id=a.login_id and b.role_id in ('sysadmsupervisorr2','sysadmsalesrepr2'); 

if v_password = v_entered_password and v_status = 'ACTIVE' then 
o_status := 'success'; 
o_message := null; 
else 
o_status := 'fail'; 

```

On a system like:

```plaintext
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.7 (jessie) 
awk GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2-p3, GNU MP 6.0.0)

```

---

<div class="post-metadata">

**Author:** ![AbelLuis](https://community.unix.com/user_avatar/community.unix.com/abelluis/32/15523_2.png) [@AbelLuis](https://community.unix.com/u/AbelLuis)\
**Post date:** [April 30, 2017, 12:48pm UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/5 "2017-04-30T12:48:38Z")

</div>

Hi, drl,  
this worked on the original sample:

```nohighlight
#!/usr/bin/awk -f

{
n1=match($0, "\x27"); 
n2=match(substr($0, n1+1), "\x27"); 

while ( n1 > 0 ){
    printf("%s", tolower(substr($0, 1, n1-1)));
    printf("%s", substr($0, n1, n2+1));
    $0=substr($0, n1+n2+1);
    n1=match($0, "\x27"); 
    n2=match(substr($0, n1+1), "\x27"); 
}
    print tolower($0);

}

```

The output was:

```nohighlight
select a.login_id,a.password,nvl(a.temp_pass_expiry_dt,systimestamp),nvl(upper(a.status),
' ')
into v_login_id, v_password , v_temp_pass_expiry_dt,v_status
from w_dealer_master a, w_dealer_user_map b
where a.login_id = i_loginid and b.login_id=a.login_id and b.role_id
in ('sysAdmSupervisorR2','sysAdmSalesRepR2');
if v_password = v_entered_password and v_status = 'ACTIVE' then
o_status := 'SUCCESS';
o_message := null;
else
o_status := 'FAIL';

```

Greetings!

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 30, 2017, 1:57pm UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/6 "2017-04-30T13:57:09Z")

</div>

Try

```nohighlight
awk -F"'" -vOFS="'" '{for (i=1; i<=NF; i+=2) $i = tolower($i)} 1 ' file
select a.login_id,a.password,nvl(a.temp_pass_expiry_dt,systimestamp),nvl(upper(a.status), ' ')
  into v_login_id, v_password , v_temp_pass_expiry_dt,v_status
  from w_dealer_master a, w_dealer_user_map b
  where a.login_id = i_loginid and b.login_id=a.login_id and b.role_id in ('sysAdmSupervisorR2','sysAdmSalesRepR2');

        if v_password = v_entered_password and v_status = 'ACTIVE' then
                        o_status := 'SUCCESS';
                        o_message := null;
                else
                    o_status := 'FAIL';

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 30, 2017, 3:18pm UTC](https://community.unix.com/t/convert-text-to-lower-case-except-the-strings-within-single-quotes/366308/7 "2017-04-30T15:18:17Z")

</div>

Try: (GNU sed):

```nohighlight
sed -r "s/([^']*)(('[^']*')?)/\L\1\E\2/g" file

```
