Filtering records of a file based on a value of a column
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
8 ZX 12 6
9 ZA 32 8
From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).
1 XX 45 N
2 YY 34 y
4 XX 89 Y
5 XX 45 N
6 YY 84 D
Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.
text-processing awk sed
add a comment |Â
up vote
2
down vote
favorite
I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
8 ZX 12 6
9 ZA 32 8
From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).
1 XX 45 N
2 YY 34 y
4 XX 89 Y
5 XX 45 N
6 YY 84 D
Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.
text-processing awk sed
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
8 ZX 12 6
9 ZA 32 8
From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).
1 XX 45 N
2 YY 34 y
4 XX 89 Y
5 XX 45 N
6 YY 84 D
Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.
text-processing awk sed
I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
8 ZX 12 6
9 ZA 32 8
From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).
1 XX 45 N
2 YY 34 y
4 XX 89 Y
5 XX 45 N
6 YY 84 D
Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.
text-processing awk sed
text-processing awk sed
edited Oct 4 at 9:18
Jeff Schaller
33.6k851113
33.6k851113
asked Oct 4 at 4:25
Shervan
32210
32210
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Let's say your data is listed in a file called file.txt
.You can try awk
as follows:
awk '$2 ~ /XX|YY|ZZ/' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
or grep
grep 'XX|YY|ZZ' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
To sort the data based on the third column, you can pipe the output data to the command sort
as follows:
grep 'XX|YY|ZZ' file.txt | sort -k3,3n
7 ZZ 22 S
2 YY 34 y
3 ZZ 44 N
1 XX 45 N
5 XX 45 N
6 YY 84 D
4 XX 89 Y
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that thegrep
variant matches the values anywhere in the line, not just in the second field.
â Stephen Kitt
Oct 4 at 4:45
1
@Goro I was only wondering why you specified the starting character (.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think-k3,3n
would be more appropriate.
â Stephen Kitt
Oct 4 at 5:12
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Let's say your data is listed in a file called file.txt
.You can try awk
as follows:
awk '$2 ~ /XX|YY|ZZ/' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
or grep
grep 'XX|YY|ZZ' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
To sort the data based on the third column, you can pipe the output data to the command sort
as follows:
grep 'XX|YY|ZZ' file.txt | sort -k3,3n
7 ZZ 22 S
2 YY 34 y
3 ZZ 44 N
1 XX 45 N
5 XX 45 N
6 YY 84 D
4 XX 89 Y
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that thegrep
variant matches the values anywhere in the line, not just in the second field.
â Stephen Kitt
Oct 4 at 4:45
1
@Goro I was only wondering why you specified the starting character (.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think-k3,3n
would be more appropriate.
â Stephen Kitt
Oct 4 at 5:12
 |Â
show 2 more comments
up vote
4
down vote
accepted
Let's say your data is listed in a file called file.txt
.You can try awk
as follows:
awk '$2 ~ /XX|YY|ZZ/' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
or grep
grep 'XX|YY|ZZ' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
To sort the data based on the third column, you can pipe the output data to the command sort
as follows:
grep 'XX|YY|ZZ' file.txt | sort -k3,3n
7 ZZ 22 S
2 YY 34 y
3 ZZ 44 N
1 XX 45 N
5 XX 45 N
6 YY 84 D
4 XX 89 Y
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that thegrep
variant matches the values anywhere in the line, not just in the second field.
â Stephen Kitt
Oct 4 at 4:45
1
@Goro I was only wondering why you specified the starting character (.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think-k3,3n
would be more appropriate.
â Stephen Kitt
Oct 4 at 5:12
 |Â
show 2 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Let's say your data is listed in a file called file.txt
.You can try awk
as follows:
awk '$2 ~ /XX|YY|ZZ/' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
or grep
grep 'XX|YY|ZZ' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
To sort the data based on the third column, you can pipe the output data to the command sort
as follows:
grep 'XX|YY|ZZ' file.txt | sort -k3,3n
7 ZZ 22 S
2 YY 34 y
3 ZZ 44 N
1 XX 45 N
5 XX 45 N
6 YY 84 D
4 XX 89 Y
Let's say your data is listed in a file called file.txt
.You can try awk
as follows:
awk '$2 ~ /XX|YY|ZZ/' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
or grep
grep 'XX|YY|ZZ' file.txt
1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
To sort the data based on the third column, you can pipe the output data to the command sort
as follows:
grep 'XX|YY|ZZ' file.txt | sort -k3,3n
7 ZZ 22 S
2 YY 34 y
3 ZZ 44 N
1 XX 45 N
5 XX 45 N
6 YY 84 D
4 XX 89 Y
edited Oct 4 at 5:25
Stephen Kitt
149k23331397
149k23331397
answered Oct 4 at 4:29
Goro
7,24753168
7,24753168
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that thegrep
variant matches the values anywhere in the line, not just in the second field.
â Stephen Kitt
Oct 4 at 4:45
1
@Goro I was only wondering why you specified the starting character (.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think-k3,3n
would be more appropriate.
â Stephen Kitt
Oct 4 at 5:12
 |Â
show 2 more comments
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that thegrep
variant matches the values anywhere in the line, not just in the second field.
â Stephen Kitt
Oct 4 at 4:45
1
@Goro I was only wondering why you specified the starting character (.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think-k3,3n
would be more appropriate.
â Stephen Kitt
Oct 4 at 5:12
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
â Shervan
Oct 4 at 4:36
Please see my edits.
â Goro
Oct 4 at 4:39
Please see my edits.
â Goro
Oct 4 at 4:39
1
1
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
Thank you so much @goro for the great help!
â Shervan
Oct 4 at 4:41
@Shervan note that the
grep
variant matches the values anywhere in the line, not just in the second field.â Stephen Kitt
Oct 4 at 4:45
@Shervan note that the
grep
variant matches the values anywhere in the line, not just in the second field.â Stephen Kitt
Oct 4 at 4:45
1
1
@Goro I was only wondering why you specified the starting character (
.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n
would be more appropriate.â Stephen Kitt
Oct 4 at 5:12
@Goro I was only wondering why you specified the starting character (
.1
), thatâÂÂs all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n
would be more appropriate.â Stephen Kitt
Oct 4 at 5:12
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473133%2ffiltering-records-of-a-file-based-on-a-value-of-a-column%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password