LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-14-2010, 01:16 PM   #1
cliffyao
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 15
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
 
Old 06-14-2010, 02:00 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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


Cheers,
Tink
 
Old 06-14-2010, 03:06 PM   #3
cliffyao
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Original Poster
Rep: Reputation: 15
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!
 
Old 06-14-2010, 03:20 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by cliffyao View Post
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
 
Old 06-14-2010, 09:20 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change all uppercase to lowercase with vi?? ufmale Linux - Newbie 4 07-05-2013 05:16 AM
UPPERCASE to lowercase command? pk21 Linux - General 4 06-12-2007 11:26 AM
Lowercase to Uppercase stellafrank Linux - Software 2 11-16-2006 06:46 AM
Converting Uppercase to Lowercase AMMullan Linux - Software 6 10-18-2005 08:32 PM
External harddrive FAT32- VFAT options (uppercase to lowercase file name translation) pauquetb Linux - General 1 03-20-2004 01:52 PM


All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration