How to remove fields with all zeros
up vote
1
down vote
favorite
I have a file
that looks like this :
header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...
I want to remove any column with all zeros like d0
I can manually inspect for columns with all zeros and find d0 and execute
cut -d "," -f 1,3- file> file_revised
The desired output is
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...
But since I have so many columns, it is hard to inspect manually.
How can I automatically remove columns with all zeros?
Thank you.
awk cut
add a comment |
up vote
1
down vote
favorite
I have a file
that looks like this :
header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...
I want to remove any column with all zeros like d0
I can manually inspect for columns with all zeros and find d0 and execute
cut -d "," -f 1,3- file> file_revised
The desired output is
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...
But since I have so many columns, it is hard to inspect manually.
How can I automatically remove columns with all zeros?
Thank you.
awk cut
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
1
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file
that looks like this :
header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...
I want to remove any column with all zeros like d0
I can manually inspect for columns with all zeros and find d0 and execute
cut -d "," -f 1,3- file> file_revised
The desired output is
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...
But since I have so many columns, it is hard to inspect manually.
How can I automatically remove columns with all zeros?
Thank you.
awk cut
I have a file
that looks like this :
header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...
I want to remove any column with all zeros like d0
I can manually inspect for columns with all zeros and find d0 and execute
cut -d "," -f 1,3- file> file_revised
The desired output is
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...
But since I have so many columns, it is hard to inspect manually.
How can I automatically remove columns with all zeros?
Thank you.
awk cut
awk cut
edited Nov 11 at 13:54
asked Nov 11 at 13:45
Sumin Kim
848
848
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
1
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54
add a comment |
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
1
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
1
1
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1
for (i=1; i<=NF; ++i)
a[i]
next
NR==FNR
for (i in a)
if ($i != "0")
delete a[i]
next
sep = ""
out = ""
for (i=1; i<=NF; ++i)
if (i in a)
continue
out = out sep $i
sep = FS
print out
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
add a comment |
up vote
1
down vote
Provided that the first column does not contain all zeros, this awk script should to the job
awk -F',' '(NR==FNR && NR >1)for(i = 1; i <= NF; i++)
a[i] = a[i]+$i
(FNR!=NR)out=$1
for(i = 2; i<= NF; i++)
if(a[i]!=0)out=out","$i
print out
' file_name file_name
Note that the sript takes the name of the input file file_name twice!
For example, for the input:
header,d0,d
s1,0,5,2,8,
s2,0,8,2,4,
s3,0,7,3,4,
s4,0,3,2,1,
the script yields as output
header,d
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
add a comment |
up vote
1
down vote
Here is one that gathers the fields to print to a variable (p="$1,$3"
... etc.) and uses system
to call awk to print p
:
$ awk '
BEGIN FS=OFS=","
NR==1
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i]
END # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGINFS=OFS=","" cmd "47 " FILENAME)
' file
Output:
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
If all fields are all zeros, p="print"
and the whole file gets printed.
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
$ sed 's/b0,b//g' test.txt
header,d0,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1
for (i=1; i<=NF; ++i)
a[i]
next
NR==FNR
for (i in a)
if ($i != "0")
delete a[i]
next
sep = ""
out = ""
for (i=1; i<=NF; ++i)
if (i in a)
continue
out = out sep $i
sep = FS
print out
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
add a comment |
up vote
1
down vote
accepted
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1
for (i=1; i<=NF; ++i)
a[i]
next
NR==FNR
for (i in a)
if ($i != "0")
delete a[i]
next
sep = ""
out = ""
for (i=1; i<=NF; ++i)
if (i in a)
continue
out = out sep $i
sep = FS
print out
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1
for (i=1; i<=NF; ++i)
a[i]
next
NR==FNR
for (i in a)
if ($i != "0")
delete a[i]
next
sep = ""
out = ""
for (i=1; i<=NF; ++i)
if (i in a)
continue
out = out sep $i
sep = FS
print out
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1
for (i=1; i<=NF; ++i)
a[i]
next
NR==FNR
for (i in a)
if ($i != "0")
delete a[i]
next
sep = ""
out = ""
for (i=1; i<=NF; ++i)
if (i in a)
continue
out = out sep $i
sep = FS
print out
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
edited Nov 11 at 14:20
answered Nov 11 at 14:07
oguzismail
2,7722823
2,7722823
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
add a comment |
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
1
1
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
It worked perfectly for me! Thank you very much.
– Sumin Kim
Nov 11 at 14:13
add a comment |
up vote
1
down vote
Provided that the first column does not contain all zeros, this awk script should to the job
awk -F',' '(NR==FNR && NR >1)for(i = 1; i <= NF; i++)
a[i] = a[i]+$i
(FNR!=NR)out=$1
for(i = 2; i<= NF; i++)
if(a[i]!=0)out=out","$i
print out
' file_name file_name
Note that the sript takes the name of the input file file_name twice!
For example, for the input:
header,d0,d
s1,0,5,2,8,
s2,0,8,2,4,
s3,0,7,3,4,
s4,0,3,2,1,
the script yields as output
header,d
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
add a comment |
up vote
1
down vote
Provided that the first column does not contain all zeros, this awk script should to the job
awk -F',' '(NR==FNR && NR >1)for(i = 1; i <= NF; i++)
a[i] = a[i]+$i
(FNR!=NR)out=$1
for(i = 2; i<= NF; i++)
if(a[i]!=0)out=out","$i
print out
' file_name file_name
Note that the sript takes the name of the input file file_name twice!
For example, for the input:
header,d0,d
s1,0,5,2,8,
s2,0,8,2,4,
s3,0,7,3,4,
s4,0,3,2,1,
the script yields as output
header,d
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
add a comment |
up vote
1
down vote
up vote
1
down vote
Provided that the first column does not contain all zeros, this awk script should to the job
awk -F',' '(NR==FNR && NR >1)for(i = 1; i <= NF; i++)
a[i] = a[i]+$i
(FNR!=NR)out=$1
for(i = 2; i<= NF; i++)
if(a[i]!=0)out=out","$i
print out
' file_name file_name
Note that the sript takes the name of the input file file_name twice!
For example, for the input:
header,d0,d
s1,0,5,2,8,
s2,0,8,2,4,
s3,0,7,3,4,
s4,0,3,2,1,
the script yields as output
header,d
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
Provided that the first column does not contain all zeros, this awk script should to the job
awk -F',' '(NR==FNR && NR >1)for(i = 1; i <= NF; i++)
a[i] = a[i]+$i
(FNR!=NR)out=$1
for(i = 2; i<= NF; i++)
if(a[i]!=0)out=out","$i
print out
' file_name file_name
Note that the sript takes the name of the input file file_name twice!
For example, for the input:
header,d0,d
s1,0,5,2,8,
s2,0,8,2,4,
s3,0,7,3,4,
s4,0,3,2,1,
the script yields as output
header,d
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
answered Nov 11 at 14:17
F. Knorr
2,337716
2,337716
add a comment |
add a comment |
up vote
1
down vote
Here is one that gathers the fields to print to a variable (p="$1,$3"
... etc.) and uses system
to call awk to print p
:
$ awk '
BEGIN FS=OFS=","
NR==1
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i]
END # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGINFS=OFS=","" cmd "47 " FILENAME)
' file
Output:
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
If all fields are all zeros, p="print"
and the whole file gets printed.
add a comment |
up vote
1
down vote
Here is one that gathers the fields to print to a variable (p="$1,$3"
... etc.) and uses system
to call awk to print p
:
$ awk '
BEGIN FS=OFS=","
NR==1
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i]
END # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGINFS=OFS=","" cmd "47 " FILENAME)
' file
Output:
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
If all fields are all zeros, p="print"
and the whole file gets printed.
add a comment |
up vote
1
down vote
up vote
1
down vote
Here is one that gathers the fields to print to a variable (p="$1,$3"
... etc.) and uses system
to call awk to print p
:
$ awk '
BEGIN FS=OFS=","
NR==1
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i]
END # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGINFS=OFS=","" cmd "47 " FILENAME)
' file
Output:
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
If all fields are all zeros, p="print"
and the whole file gets printed.
Here is one that gathers the fields to print to a variable (p="$1,$3"
... etc.) and uses system
to call awk to print p
:
$ awk '
BEGIN FS=OFS=","
NR==1
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i]
END # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGINFS=OFS=","" cmd "47 " FILENAME)
' file
Output:
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
If all fields are all zeros, p="print"
and the whole file gets printed.
answered Nov 11 at 16:10
James Brown
17.6k31635
17.6k31635
add a comment |
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
$ sed 's/b0,b//g' test.txt
header,d0,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
add a comment |
up vote
0
down vote
maybe you can use sed
command like below:
$ sed 's/b0,b//g' test.txt
header,d0,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
add a comment |
up vote
0
down vote
up vote
0
down vote
maybe you can use sed
command like below:
$ sed 's/b0,b//g' test.txt
header,d0,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
maybe you can use sed
command like below:
$ sed 's/b0,b//g' test.txt
header,d0,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1
answered Nov 11 at 14:48
GerryLon
444
444
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
add a comment |
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
– oguzismail
Nov 11 at 15:05
yes, if zero is also in the last column, then you can try
sed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
yes, if zero is also in the last column, then you can try
sed 's/b0,b|b,0b//g'
– GerryLon
Nov 12 at 4:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53249372%2fhow-to-remove-fields-with-all-zeros%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
Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47
And what you already tried yourself
– Ivonet
Nov 11 at 13:49
1
I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54