How to extract lines when two columns strings are not equal
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I have a CSV file that looks like this format:
text1,text2,string1,string2
text3,text3,string3,string2
text4,text5,string1,string2
text6,text6,string6,string7
I want to extract rows when column1 and column2 are not equal. The expected result in the above example would be:
text1,text2,string1,string2
text4,text5,string1,string2
When column1 and column2 are not equal. I am familiar with commands that allow me extract specific column like the following to extract the first column:
cat input.csv | cut -d ',' -f1 > output.csv
text-processing csv-simple
add a comment |
up vote
-1
down vote
favorite
I have a CSV file that looks like this format:
text1,text2,string1,string2
text3,text3,string3,string2
text4,text5,string1,string2
text6,text6,string6,string7
I want to extract rows when column1 and column2 are not equal. The expected result in the above example would be:
text1,text2,string1,string2
text4,text5,string1,string2
When column1 and column2 are not equal. I am familiar with commands that allow me extract specific column like the following to extract the first column:
cat input.csv | cut -d ',' -f1 > output.csv
text-processing csv-simple
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a CSV file that looks like this format:
text1,text2,string1,string2
text3,text3,string3,string2
text4,text5,string1,string2
text6,text6,string6,string7
I want to extract rows when column1 and column2 are not equal. The expected result in the above example would be:
text1,text2,string1,string2
text4,text5,string1,string2
When column1 and column2 are not equal. I am familiar with commands that allow me extract specific column like the following to extract the first column:
cat input.csv | cut -d ',' -f1 > output.csv
text-processing csv-simple
I have a CSV file that looks like this format:
text1,text2,string1,string2
text3,text3,string3,string2
text4,text5,string1,string2
text6,text6,string6,string7
I want to extract rows when column1 and column2 are not equal. The expected result in the above example would be:
text1,text2,string1,string2
text4,text5,string1,string2
When column1 and column2 are not equal. I am familiar with commands that allow me extract specific column like the following to extract the first column:
cat input.csv | cut -d ',' -f1 > output.csv
text-processing csv-simple
text-processing csv-simple
edited Nov 25 at 13:58
Rui F Ribeiro
38.3k1476127
38.3k1476127
asked Nov 25 at 12:23
user9371654
2336
2336
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Assuming that this is a simple CSV file, without any fancy embedding of commas within the fields of the actual data, you may use awk
to do this:
awk -F ',' '$1 != $2' <input.csv
This is a shorthand way of writing
awk 'BEGIN FS = "," $1 != $2 print ' <input.csv
and it sets the input field separator to a comma and prints each line if the first and second field ($1
and $2
) are not identical.
An equivalent Perl variant:
perl -F ',' -na -e 'print if $F[0] ne $F[1]' <input.csv
Good description. Even shorter if strip unnecessary quotes/spaces/direction :awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
add a comment |
up vote
1
down vote
GNU sed
solution:
sed -E '/^([^,]+,)1/d' input.csv
The output:
text1,text2,string1,string2
text4,text5,string1,string2
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
add a comment |
up vote
0
down vote
$ awk -F "," 'if ($1 != $2)print $0' filename
text1,text2,string1,string2
text4,text5,string1,string2
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Assuming that this is a simple CSV file, without any fancy embedding of commas within the fields of the actual data, you may use awk
to do this:
awk -F ',' '$1 != $2' <input.csv
This is a shorthand way of writing
awk 'BEGIN FS = "," $1 != $2 print ' <input.csv
and it sets the input field separator to a comma and prints each line if the first and second field ($1
and $2
) are not identical.
An equivalent Perl variant:
perl -F ',' -na -e 'print if $F[0] ne $F[1]' <input.csv
Good description. Even shorter if strip unnecessary quotes/spaces/direction :awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
add a comment |
up vote
2
down vote
accepted
Assuming that this is a simple CSV file, without any fancy embedding of commas within the fields of the actual data, you may use awk
to do this:
awk -F ',' '$1 != $2' <input.csv
This is a shorthand way of writing
awk 'BEGIN FS = "," $1 != $2 print ' <input.csv
and it sets the input field separator to a comma and prints each line if the first and second field ($1
and $2
) are not identical.
An equivalent Perl variant:
perl -F ',' -na -e 'print if $F[0] ne $F[1]' <input.csv
Good description. Even shorter if strip unnecessary quotes/spaces/direction :awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Assuming that this is a simple CSV file, without any fancy embedding of commas within the fields of the actual data, you may use awk
to do this:
awk -F ',' '$1 != $2' <input.csv
This is a shorthand way of writing
awk 'BEGIN FS = "," $1 != $2 print ' <input.csv
and it sets the input field separator to a comma and prints each line if the first and second field ($1
and $2
) are not identical.
An equivalent Perl variant:
perl -F ',' -na -e 'print if $F[0] ne $F[1]' <input.csv
Assuming that this is a simple CSV file, without any fancy embedding of commas within the fields of the actual data, you may use awk
to do this:
awk -F ',' '$1 != $2' <input.csv
This is a shorthand way of writing
awk 'BEGIN FS = "," $1 != $2 print ' <input.csv
and it sets the input field separator to a comma and prints each line if the first and second field ($1
and $2
) are not identical.
An equivalent Perl variant:
perl -F ',' -na -e 'print if $F[0] ne $F[1]' <input.csv
edited Nov 25 at 14:34
answered Nov 25 at 12:30
Kusalananda
118k16223361
118k16223361
Good description. Even shorter if strip unnecessary quotes/spaces/direction :awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
add a comment |
Good description. Even shorter if strip unnecessary quotes/spaces/direction :awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
Good description. Even shorter if strip unnecessary quotes/spaces/direction :
awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
Good description. Even shorter if strip unnecessary quotes/spaces/direction :
awk -F, '$1!=$2' input.csv
– steve
Nov 25 at 13:47
add a comment |
up vote
1
down vote
GNU sed
solution:
sed -E '/^([^,]+,)1/d' input.csv
The output:
text1,text2,string1,string2
text4,text5,string1,string2
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
add a comment |
up vote
1
down vote
GNU sed
solution:
sed -E '/^([^,]+,)1/d' input.csv
The output:
text1,text2,string1,string2
text4,text5,string1,string2
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
add a comment |
up vote
1
down vote
up vote
1
down vote
GNU sed
solution:
sed -E '/^([^,]+,)1/d' input.csv
The output:
text1,text2,string1,string2
text4,text5,string1,string2
GNU sed
solution:
sed -E '/^([^,]+,)1/d' input.csv
The output:
text1,text2,string1,string2
text4,text5,string1,string2
edited Nov 25 at 13:01
Kusalananda
118k16223361
118k16223361
answered Nov 25 at 12:42
RomanPerekhrest
22.7k12246
22.7k12246
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
add a comment |
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
Can you please provide explanation. Does this command compares the two columns and print the different column1 and column2? Because I do not care about column3 or column4 at all. The may be different or similar. I do not consider them.
– user9371654
Nov 25 at 14:17
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
@user9371654 The command uses a regular expression that will only match those lines that have equal values in the first and second column (literally: "any string not containing a comma, followed by a comma and the same string and comma again"). The lines that matches are removed from the input, and only the lines that you'd like to see are let through.
– Kusalananda
Nov 25 at 14:49
add a comment |
up vote
0
down vote
$ awk -F "," 'if ($1 != $2)print $0' filename
text1,text2,string1,string2
text4,text5,string1,string2
add a comment |
up vote
0
down vote
$ awk -F "," 'if ($1 != $2)print $0' filename
text1,text2,string1,string2
text4,text5,string1,string2
add a comment |
up vote
0
down vote
up vote
0
down vote
$ awk -F "," 'if ($1 != $2)print $0' filename
text1,text2,string1,string2
text4,text5,string1,string2
$ awk -F "," 'if ($1 != $2)print $0' filename
text1,text2,string1,string2
text4,text5,string1,string2
edited Nov 25 at 14:50
Jeff Schaller
37k1052121
37k1052121
answered Nov 25 at 13:40
Praveen Kumar BS
1,168138
1,168138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484025%2fhow-to-extract-lines-when-two-columns-strings-are-not-equal%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown