Grep and remove lines with a blank field value
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I was wondering if there's any command to look for blank field values of a text file and remove the entire line.
Example
Hello:Its Me
Hello:How are you
Hello:
Hello:Bye
And expected output
Hello:Its Me
Hello:How are you
Hello:Bye
text-processing awk grep cut
add a comment |Â
up vote
2
down vote
favorite
I was wondering if there's any command to look for blank field values of a text file and remove the entire line.
Example
Hello:Its Me
Hello:How are you
Hello:
Hello:Bye
And expected output
Hello:Its Me
Hello:How are you
Hello:Bye
text-processing awk grep cut
3
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I was wondering if there's any command to look for blank field values of a text file and remove the entire line.
Example
Hello:Its Me
Hello:How are you
Hello:
Hello:Bye
And expected output
Hello:Its Me
Hello:How are you
Hello:Bye
text-processing awk grep cut
I was wondering if there's any command to look for blank field values of a text file and remove the entire line.
Example
Hello:Its Me
Hello:How are you
Hello:
Hello:Bye
And expected output
Hello:Its Me
Hello:How are you
Hello:Bye
text-processing awk grep cut
edited Dec 13 '17 at 20:57
terdonâ¦
122k28230403
122k28230403
asked Dec 13 '17 at 16:05
John Wick
112
112
3
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21
add a comment |Â
3
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21
3
3
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
5
down vote
Very standard case, the simplest solution with awk
would be
awk -F: '$2 != ""' file
and with grep
:
grep :. file
add a comment |Â
up vote
3
down vote
If you mean to skip Hello:
lines you can use something like:
awk -F: 'if($2 != "") print ' input_file
add a comment |Â
up vote
2
down vote
At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true
is to print. So, if you set the field delimiter to :
, to print lines that have a second field, all you need is:
$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye
However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0
s, because awk
evaluates this as false.
add a comment |Â
up vote
0
down vote
Sometimes it is easier to define the remove conditions (grep -v
). Example remove when last field is empty:
grep -v ':$' file
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Very standard case, the simplest solution with awk
would be
awk -F: '$2 != ""' file
and with grep
:
grep :. file
add a comment |Â
up vote
5
down vote
Very standard case, the simplest solution with awk
would be
awk -F: '$2 != ""' file
and with grep
:
grep :. file
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Very standard case, the simplest solution with awk
would be
awk -F: '$2 != ""' file
and with grep
:
grep :. file
Very standard case, the simplest solution with awk
would be
awk -F: '$2 != ""' file
and with grep
:
grep :. file
edited Dec 13 '17 at 16:15
Stéphane Chazelas
282k53520854
282k53520854
answered Dec 13 '17 at 16:10
jimmij
28.9k867100
28.9k867100
add a comment |Â
add a comment |Â
up vote
3
down vote
If you mean to skip Hello:
lines you can use something like:
awk -F: 'if($2 != "") print ' input_file
add a comment |Â
up vote
3
down vote
If you mean to skip Hello:
lines you can use something like:
awk -F: 'if($2 != "") print ' input_file
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If you mean to skip Hello:
lines you can use something like:
awk -F: 'if($2 != "") print ' input_file
If you mean to skip Hello:
lines you can use something like:
awk -F: 'if($2 != "") print ' input_file
answered Dec 13 '17 at 16:08
Romeo Ninov
4,36811625
4,36811625
add a comment |Â
add a comment |Â
up vote
2
down vote
At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true
is to print. So, if you set the field delimiter to :
, to print lines that have a second field, all you need is:
$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye
However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0
s, because awk
evaluates this as false.
add a comment |Â
up vote
2
down vote
At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true
is to print. So, if you set the field delimiter to :
, to print lines that have a second field, all you need is:
$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye
However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0
s, because awk
evaluates this as false.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true
is to print. So, if you set the field delimiter to :
, to print lines that have a second field, all you need is:
$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye
However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0
s, because awk
evaluates this as false.
At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true
is to print. So, if you set the field delimiter to :
, to print lines that have a second field, all you need is:
$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye
However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0
s, because awk
evaluates this as false.
edited Dec 13 '17 at 23:04
jimmij
28.9k867100
28.9k867100
answered Dec 13 '17 at 20:59
terdonâ¦
122k28230403
122k28230403
add a comment |Â
add a comment |Â
up vote
0
down vote
Sometimes it is easier to define the remove conditions (grep -v
). Example remove when last field is empty:
grep -v ':$' file
add a comment |Â
up vote
0
down vote
Sometimes it is easier to define the remove conditions (grep -v
). Example remove when last field is empty:
grep -v ':$' file
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Sometimes it is easier to define the remove conditions (grep -v
). Example remove when last field is empty:
grep -v ':$' file
Sometimes it is easier to define the remove conditions (grep -v
). Example remove when last field is empty:
grep -v ':$' file
answered Dec 14 '17 at 10:17
JJoao
6,7211826
6,7211826
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%2f410679%2fgrep-and-remove-lines-with-a-blank-field-value%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
3
do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
â Sundeep
Dec 13 '17 at 16:21