LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to separate lowercase and uppercase in my file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-separate-lowercase-and-uppercase-in-my-file-814087/)

cliffyao 06-14-2010 12:16 PM

How to separate lowercase and uppercase in my file
 
Hi, All

I have a file partially like this

209 c 89
215 a 76
603 B 190
626 f 240

I want to separate this file into two sub-files based on the lowercase and uppercase of column 2 so that the sub-files are like

file1

209 c 89
215 a 76
626 f 240

file2

603 B 190

Can anyone help me how to do that?

Thanks very much

Tinkster 06-14-2010 01:00 PM

Code:

awk '{if($2 ~ /[a-z]/){print $0 > FILENAME"_l" }else{print $0 > FILENAME"_u"}}' file


Cheers,
Tink

cliffyao 06-14-2010 02:06 PM

Hi, Tink

Thanks for your quick response. I slightly modified your command to

awk '$2 ~ /[a-z]/' input.txt > output.txt

And the output.txt is the same as the input.txt..

I understood that you were using regular expression so I googled and changed the command again to

awk '$2 ~ /[[:lower:]]/' input.txt > output_lower.txt
awk '$2 ~ /[[:upper:]]/' input.txt > output_upper.txt

And now, it works.

Although I got what I wanted now, I am still curious why [a-z] can't work in my case.

Also, I noticed that I had to use double-quoted [[:lower:]] to make it work. I am wondering why single quote can't work. I searched regular expression online and it looks like

[:upper:] Any alpha character A to Z.
[:lower:] Any alpha character a to z.

Thanks again!

Tinkster 06-14-2010 02:20 PM

Quote:

Originally Posted by cliffyao (Post 4003405)
Hi, Tink

Thanks for your quick response. I slightly modified your command to

awk '$2 ~ /[a-z]/' input.txt > output.txt

And the output.txt is the same as the input.txt..

I understood that you were using regular expression so I googled and changed the command again to

awk '$2 ~ /[[:lower:]]/' input.txt > output_lower.txt
awk '$2 ~ /[[:upper:]]/' input.txt > output_upper.txt

And now, it works.

Although I got what I wanted now, I am still curious why [a-z] can't work in my case.

Also, I noticed that I had to use double-quoted [[:lower:]] to make it work. I am wondering why single quote can't work. I searched regular expression online and it looks like

[:upper:] Any alpha character A to Z.
[:lower:] Any alpha character a to z.

Thanks again!

Why a-z didn't work for you I can't answer, maybe an
encoding thing? As for the [[ ... that's a requirement.
[:lower:] is the equivalent of a-z on ASCII or iso8859-1
system - note that a-z there doesn't have the [] around
it; hence you still need the outer square bracket to make
it a range.



Cheers,
Tink

grail 06-14-2010 08:20 PM

Well assuming the input only ever has alpha in column 2 you could just go with checking the whole line, so something like this can work;
Code:

awk '/[[:lower:]]/{print > "lower";next}1' input > upper


All times are GMT -5. The time now is 04:21 AM.