print lines with blank spaces or white spaces

56 Views Asked by At

I am having dificulty trying to print lines with white space only and excluding the rest like below.

Inside test.txt

francoiv|538|
gerardl|520|
icell|548|
icell  |787|
ieppcnf|791|
ieppix |788|
ieppvas|792|
ifastrk|790|
iinter|558|
iinter |793|
ixaeppix|795|
ixaepprp|796|
ixaint|772|
ixaint |794|
ixarocel|771|
ixaroint|773|
karsc001|1051|
mafokokg|535|
makgotma|528|

The expected output I require is below.

icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

I have tried so many sed and awk options online and I can't seem to come right. The number of white spaces can be more than 2 so I was trying to use a command that can find before the pipe [|] a whitespace or blank character, but I can't find a way to do this.

When I use cat -v test.txt to display special character, I get below.

francoiv|538|
gerardl|520|
icell|548|
icellM- M- |787|
ieppcnf|791|
ieppixM- |788|
ieppvas|792|
ifastrk|790|
iinter|558|
iinterM- |793|
ixaeppix|795|
ixaepprp|796|
ixaint|772|
ixaintM- |794|
ixarocel|771|
ixaroint|773|
karsc001|1051|
mafokokg|535|
makgotma|528|

Not sure what M- means

4

There are 4 best solutions below

8
James Brown On

If the space is in the end of the first field, use:

$ awk -F\| '$1~/ +$/' file
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

Explained:

awk -F\| using awk, pipe is the field separator

$1 first field

~/ +$/ ends in one or more spaces

0
Daweo On

GNU sed solution, let file.txt content be

francoiv|538|
gerardl|520|
icell|548|
icell  |787|
ieppcnf|791|
ieppix |788|
ieppvas|792|
ifastrk|790|
iinter|558|
iinter |793|
ixaeppix|795|
ixaepprp|796|
ixaint|772|
ixaint |794|
ixarocel|771|
ixaroint|773|
karsc001|1051|
mafokokg|535|
makgotma|528|

then

sed '/[a-z0-9]|[0-9]/d' file.txt

gives output

icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

Explanation: I instruct GNU sed to delete lines when there is lowercase ascii letter or digit followed by pipe followed by digit. Others lines are printed (default action).

(tested in GNU sed 4.8)

0
Ed Morton On

Some possibilities (tested with GNU tools):

$ grep '[^[:alnum:]|]' test.txt
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

$ grep '[^[:graph:]]' test.txt
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

$ awk '/[^\x00-\x7F]/' test.txt
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|
1
markp-fuso On

Print only lines that contain (at least) one space:

$ grep ' ' test.txt
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|

If you mean white space (ie, spaces, tabs) then:

$ grep -E '[[:space:]]' test.txt
icell  |787|
ieppix |788|
iinter |793|
ixaint |794|