Is the an elegant way to select rows in a file where a given column has a given value?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file with a lot of lines like this
0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
0.003693853427895981
and I want to select all rows where the entry in the third columns equals to some given number.
I know how to write a pattern for grep -E
for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.
P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.
text-processing csv coreutils
add a comment |
up vote
0
down vote
favorite
I have a file with a lot of lines like this
0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
0.003693853427895981
and I want to select all rows where the entry in the third columns equals to some given number.
I know how to write a pattern for grep -E
for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.
P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.
text-processing csv coreutils
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file with a lot of lines like this
0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
0.003693853427895981
and I want to select all rows where the entry in the third columns equals to some given number.
I know how to write a pattern for grep -E
for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.
P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.
text-processing csv coreutils
I have a file with a lot of lines like this
0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
0.003693853427895981
and I want to select all rows where the entry in the third columns equals to some given number.
I know how to write a pattern for grep -E
for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.
P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.
text-processing csv coreutils
text-processing csv coreutils
asked 15 hours ago
jknappen
258113
258113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
Awk
is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N
, where N
is the last column number in the line.
So to your requirement just match the third column with the value you need
awk '$3 == "string"' file
If the file is a CSV which has a different de-limiter, e.g. ,
just define it as
awk -v FS="," '$3 == "string"' file
Also awk
is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Awk
is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N
, where N
is the last column number in the line.
So to your requirement just match the third column with the value you need
awk '$3 == "string"' file
If the file is a CSV which has a different de-limiter, e.g. ,
just define it as
awk -v FS="," '$3 == "string"' file
Also awk
is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.
add a comment |
up vote
3
down vote
Awk
is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N
, where N
is the last column number in the line.
So to your requirement just match the third column with the value you need
awk '$3 == "string"' file
If the file is a CSV which has a different de-limiter, e.g. ,
just define it as
awk -v FS="," '$3 == "string"' file
Also awk
is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.
add a comment |
up vote
3
down vote
up vote
3
down vote
Awk
is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N
, where N
is the last column number in the line.
So to your requirement just match the third column with the value you need
awk '$3 == "string"' file
If the file is a CSV which has a different de-limiter, e.g. ,
just define it as
awk -v FS="," '$3 == "string"' file
Also awk
is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.
Awk
is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N
, where N
is the last column number in the line.
So to your requirement just match the third column with the value you need
awk '$3 == "string"' file
If the file is a CSV which has a different de-limiter, e.g. ,
just define it as
awk -v FS="," '$3 == "string"' file
Also awk
is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.
edited 15 hours ago
answered 15 hours ago
Inian
3,695823
3,695823
add a comment |
add a comment |
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%2f481275%2fis-the-an-elegant-way-to-select-rows-in-a-file-where-a-given-column-has-a-given%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