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'll detect the third line.
This is what Im doing.
grep -E '" "" "+' $dname".txt" >> $dname"_error.txt"
The logic on which I'm basing this is, the first white space must be followed by one or more white spaces to be detected as an error.
$dname is a variable that holds the filename path.
How do I get my desired output?
( which is
3|Her mi oh ne|is|silent
)
For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match.
Re: the echo $string | perl note: When IFS is at its default value, echo $string itself will replace runs of multiple spaces with a single space (along with other side effects, like replacing a wildcard in that string with a list of files) -- one needs to use echo "$string" to keep them in.
' \s ' Match whitespace, it is a synonym for ' [[:space:]] '. ' \S ' Match non-whitespace, it is a synonym for ' [^[:space:]] '. For example, ' \brat\b ' matches the separate word ' rat ', ' \Brat\B ' matches ' crate ' but not ' furry rat '.
If you want to allow for ANY space character (tab, space, newline, etc), then if you have a “grep” that supports EXTENDED regular expressions (with the '-E' option), you can use '[[:space:]]' to represent any space character.
grep '[[:space:]]\{2,\}' ${dname}.txt >> ${dname}_error.txt
If you want to catch 2 or more whitespaces.
Just this will do:
grep " " ${dname}.txt >> ${dname}_error.txt
The two spaces in a quoted string work fine. The -E
turns the pattern into an extended regular expression, which makes this needlessly complicated here.
below are the 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With