LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-18-2010, 12:27 AM   #1
mauludi
LQ Newbie
 
Registered: Jan 2010
Posts: 21

Rep: Reputation: 0
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
 
Old 01-18-2010, 12:32 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 01-18-2010, 12:36 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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 12:38 AM.
 
Old 01-18-2010, 01:13 AM   #4
mauludi
LQ Newbie
 
Registered: Jan 2010
Posts: 21

Original Poster
Rep: Reputation: 0
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
 
Old 01-18-2010, 01:33 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Code:
awk '{ if ( $2==4 || $2==5 || FNR==1 ) print $0 }' mydata.text
Evo2.
 
Old 01-18-2010, 11:07 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 11:08 AM.
 
Old 01-18-2010, 05:52 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by Tinkster View Post
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 05:55 PM.
 
  


Reply



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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
[SOLVED] Filter through line/s to grab specific fields/data in the line with example shayno90 Linux - Newbie 11 10-14-2009 11:51 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Unix command to delete the previous and next line of the searched pattern navin29 Linux - Newbie 7 03-14-2008 01:23 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:40 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
Open Source Consulting | Domain Registration