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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-18-2010, 01:27 AM
|
#1
|
LQ Newbie
Registered: Jan 2010
Posts: 21
Rep:
|
How to delete/grab a line which matchs a pattern of a particular column only ?
Dear All,
I indeed need your help. It is very importan for my research work.
I have found many information about how to delete/grab a line in a text file including delete line with match a pattern but I did not find info about how to delete a line which match a pattern of a particular column only.
for example mydata.txt:
id type x y z
1 6 0.474611 0.227223 0.583947
2 4 0.422894 0.22726 0.536791
3 5 0.448963 0.200148 0.560336
4 3 0.386478 0.207721 0.515293
5 6 0.371617 0.22361 0.582206
6 4 0.32123 0.222999 0.534782
so how to obtain data with type 4 only so that mydata.text file become:
id type x y z
2 4 0.422894 0.22726 0.536791
6 4 0.32123 0.222999 0.534782
and how to have data without type 3 , mydata.test file become:
id type x y z
1 6 0.474611 0.227223 0.583947
2 4 0.422894 0.22726 0.536791
3 5 0.448963 0.200148 0.560336
5 6 0.371617 0.22361 0.582206
6 4 0.32123 0.222999 0.534782
thank a lot in advance
Mauludi
|
|
|
01-18-2010, 01:32 AM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Hi, welcome to LQ!
awk is the perfect tool for this kind of job.
It allows you to compare regex against column type data (e.g.,
whitespace delimited text like yours).
Code:
awk '$2 ~ /4/' file > new_file
awk '$2 !~ /3/' file > new_file
Cheers,
Tink
|
|
|
01-18-2010, 01:36 AM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Code:
awk '{ if ( $2==4 || FNR==1 ) print $0 }' mydata.text
This will print the first line or anyline when the 2nd column is 4.
Cheers,
Evo2.
Last edited by evo2; 01-18-2010 at 01:38 AM.
|
|
|
01-18-2010, 02:13 AM
|
#4
|
LQ Newbie
Registered: Jan 2010
Posts: 21
Original Poster
Rep:
|
thank you for your quick reply,
but sorry for further question, how about obtain two type (for example 4 and 5) of my datafile.tx
id type x y z
1 6 0.474611 0.227223 0.583947
2 4 0.422894 0.22726 0.536791
3 5 0.448963 0.200148 0.560336
4 3 0.386478 0.207721 0.515293
5 6 0.371617 0.22361 0.582206
6 4 0.32123 0.222999 0.534782
so that it becomes :
2 4 0.422894 0.22726 0.536791
3 5 06 4 0.32123 0.222999 0.534782
6 4 0.32123 0.222999 0.534782
once agaiin thank you very much for your help
|
|
|
01-18-2010, 02:33 AM
|
#5
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Code:
awk '{ if ( $2==4 || $2==5 || FNR==1 ) print $0 }' mydata.text
Evo2.
|
|
|
01-18-2010, 12:07 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
You just don't need either the "if" or the "print".
Code:
awk '$2==4 || $2==5 || FNR==1' mydata.text
Last edited by Tinkster; 01-18-2010 at 12:08 PM.
|
|
|
01-18-2010, 06:52 PM
|
#7
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by Tinkster
You just don't need either the "if" or the "print".
|
Very true, but by including an example with them the OP will learn more.
Cheers,
EVo2.
Last edited by evo2; 01-18-2010 at 06:55 PM.
|
|
|
All times are GMT -5. The time now is 03:48 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|