How to find lines containing more than one space between lines in unix?

I have lines like

1|Harry|says|hi 2|Ron|says|bye 3|Her mi oh ne|is|silent 4|The|above|sentence|is|weird 

I need a grep command that will detect the third line.

This is what I do.

 grep -E '" "" "+' $dname".txt" >> $dname"_error.txt" 

The logic that I base this on is that the first space should be followed by one or more white spaces that will be detected as an error.

$ dname is a variable that contains the path to the file name.

How to get the desired result?

(which is

  3|Her mi oh ne|is|silent 

)

+6
source share
4 answers

Only this will do:

 grep " " ${dname}.txt >> ${dname}_error.txt 

Two spaces in the quoted string work fine. -E turns the pattern into an extended regular expression, which does so without too much difficulty here.

+4
source
 grep '[[:space:]]\{2,\}' ${dname}.txt >> ${dname}_error.txt 

If you want to catch 2 or more spaces.

+7
source

The following are four ways.

 pearl.268> sed -n 's/ /&/p' ${dname}.txt >> ${dname}_error.txt pearl.269> awk '$0~/ /{print $0}' ${dname}.txt >> ${dname}_error.txt pearl.270> grep ' ' ${dname}.txt >> ${dname}_error.txt pearl.271> perl -ne '/ / && print' ${dname}.txt >> ${dname}_error.txt 
+1
source

If you want 2 or more spaces, then:

 grep -E "\s{2,}" ${dname}.txt >> ${dname}_error.txt 

The reason your template doesn't work is because of the quotes inside. \s used for [space]. You could do the same:

 grep -E ' +' ${dname}.txt >> ${dname}_error.txt 

But it's hard to say exactly what you are looking for with this version. \s\s+ will also work, but \s{2,} is the most concise, and also makes it possible to set an upper limit. If you want to find 2, 3 or 4 spaces in a row, you must use \s{2,4}

0
source

Source: https://habr.com/ru/post/910898/


All Articles