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 |
Code:
awk '{if($2 ~ /[a-z]/){print $0 > FILENAME"_l" }else{print $0 > FILENAME"_u"}}' file Cheers, Tink |
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! |
Quote:
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 |
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 10:11 PM. |