Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus :

It would be really appreciative if can find a solution for this .

I have records in a file .
I need to Capitalize the records based on condition .

For Example i tried the following Command

COMMAND -->

fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '[A-Z]' '[a-z]' > Buzz

The problem i am facing is this command only picks the data which matches the condition and writes into the file buzz.
I really needed the whole file with the changes .
Can Sed or awk needs to be used .

Below is the sample of the data file.

Z03741                   67595                                                             1266767IUFIUFJJJ                                   aaaa bbbbb cccc dd. xxxxxxx                 aaaa bbb cccc, Inc.    Tst .                 Floor 2                                                               El Segundo                    847474     sdfd df jjdkdkd                   dfdfd ssdfsdfsdf                            CA                                                                                   
Z03741                   67595                                                             1220CNBT1 

===================================

Thanks
B-

to use the entire file, give tr the entire file.

tr '[A-Z]' '[a-z]' < /export/home/oracle/TST/data.dat > newfile

data.dat and newfile should not be the same file, tr doesn't edit in-place.

1 Like

Show a sample of input and output.

Is there a command that does edit in-place ?

Thanks
B-

You will say i am nitpicking but using character class would make the code more reliable (avoid unexpected translation with some locale)

tr '[:upper:]' '[:lower:]' < /export/home/oracle/TST/data.dat > newfile

... by the way i don't know about your fgrep implementation, but i would bet there is the -i option available to make you grep ignore case .
so fgrep -i yuyu .... will grep any combination like

YuYu 
YUyu 
yuyU 

... or whatever

1 Like

Do you really want to edit in-place? If anything goes wrong you've destroyed your original, not a copy!

You can't just redirect into the same file you're reading from either, because you'd end up deleting the file before you even begin reading from it. Hence why this, and most shell utilities, don't edit in-place. Modifying the original is easy enough without that:

tr '[A-Z]' '[a-z]' < /export/home/oracle/TST/data.dat > /tmp/$$
cat /tmp/$$ > /export/home/oracle/TST/data.dat
rm /tmp/$$

'cat' is better than 'mv' for this because it won't ever alter data.dat's ownership or permissions.

@Shamrock :

INPUT FILE (DATA.DAT)

222222     1    A1B1         aaaaabbbbb          ccccccccc
333333     6    A2B2         xxxxxxyyyy            dddddddddd
555555          Q222         hdhdhdh3k3k          jvjvmvkfkfkfk
fgrep "A2B2" /export/home/oracle/TST/data.dat | tr '[a-z]' '[A-Z]' > Buzz

OUTPUT FILE (Buzz) from the above command

333333     6    A2B2         XXXXXYYYYY            DDDDDDDDDD

OUTPUT DESIRED

222222     1    A1B1         aaaaabbbbb          ccccccccc
333333     6    A2B2         XXXXXYYYYY            DDDDDDDDDD
555555          Q222         hdhdhdh3k3k          jvjvmvkfkfkfk

Thanks
B-

Why do you need to make those line uppercase ?

If it is for further matching, you could just use the -i option of fgrep/grep

Gurus :

Following is the SAMPLE

INPUT FILE (DATA.DAT)

222222 1 A1B1 aaaaabbbbb ccccccccc
333333 6 A2B2 xxxxxxyyyy dddddddddd
555555 Q222 hdhdhdh3k3k jvjvmvkfkfkfk
fgrep "A2B2" /export/home/oracle/TST/data.dat | tr '[a-z]' '[A-Z]' > Buzz

OUTPUT FILE (Buzz) from the above command

333333 6 A2B2 XXXXXYYYYY DDDDDDDDDD

OUTPUT DESIRED

222222 1 A1B1 aaaaabbbbb ccccccccc
333333 6 A2B2 XXXXXYYYYY DDDDDDDDDD
555555 Q222 hdhdhdh3k3k jvjvmvkfkfkfk

====================================================

Is there any other option to get the above mentioned output

Thanks
B-

yes you can do it in awk...

awk '{printf("%s\n",$0~"A2B2"?toupper($0):$0)}' file

Hello :

awk '{printf("%s\n",$0~"A2B2"?toupper($0):$0)}' file

Its spitting out the followign error

awk: syntax error near line 1
awk : illegal statement near line 1

use nawk on Solaris

1 Like

@Shamrock

Thanks a lot ! It worked like a charm :slight_smile: