TL;DR
To me, the easiest way to get the suggested results would be to replace the newlines with sed, prior to piping to to grep (i.e. fold). Then, unfold if necessary.
$ echo -e '"Random text with certain length\n"\n"Random text with certain length"\n' | sed -e ':a;N;$!ba;s/\n"/+"/g' -e '/"+/s//"\n/g' | grep -E "^.{33}$"
"Random text with certain length"
$ echo -e '"Random text with certain length\n"\n"Random text with certain length"\n' | sed -e ':a;N;$!ba;s/\n"/+"/g' -e '/"+/s//"\n/g' | grep -E "^.{34}$"
"Random text with certain length+"
$ echo -e '"Random text with certain length\n"\n"Random text with certain length"\n' | sed -e ':a;N;$!ba;s/\n"/+"/g' -e '/"+/s//"\n/g' | grep -E "^.{34}$" | sed -e '/+"/s//\n"/g'
"Random text with certain length
"
Thanks for clarifying the description. Some of what follows was in reference to the previous description, but seems like a waste to delete ...
I'm not sure I fully understand and made some assumptions.
- The lines all have double quotes, or at least something unique to fold/unfold the newlines you want to count.
- Either CR+LF or LF alone are what's being considered a 'newline/linebreak'
- In the description, \n (LF/$) could mean \r (CR/^M). That works with the reference to
wc
. Otherwise both grep
and wc
would not consider the lines the same length.
In other words, as stated, by default grep
doesn't count newline (\n) as a character but does count carriage return (\r), whereas wc
counts both as a character.
This affirms \n = newline ($) and \r = carriage return (^M)
\n = newline
$ echo -en '\n' | wc -c
1
$ echo -en '\n' | grep -E "^.{1}" | wc -c
0
\r = carriage return
$ echo -en '\r' | wc -c
1
$ echo -en '\r' | grep -E "^.{1}" | wc -c
2
To grep
, carriage returns are an extra character. Newlines are not.
This will produce the same character count & result for both lines.
echo -en '\n' | sed -e '/\r/s///g' | grep -E "^.{1}" | wc -c
0
echo -en '\r' | sed -e '/\r/s///g' | grep -E "^.{1}" | wc -c
0
Given the criteria to filter by line length, by itself grep -E
will never count a newline/LF as a character and therefore can't do it. Another example where both lines are visually the same length, but aren't actually the same length ...
$ echo -e 'hello\r\nworld\n'
hello
world
$ cat <<< "$(echo -e 'hello\r\nworld\n' | grep -E "^.{5}$")"
world
$ cat <<< "$(echo -e 'hello\r\nworld\n' | grep -E "^.{6}$")"
hello
... and inserting sed
into the pipeline, both lines are of equal length {5}:
$ cat <<< "$(echo -e 'hello\r\nworld\n' | sed -e '/\r/s///g' | grep -E "^.{5}$")"
hello
world
$ cat <<< "$(echo -e 'hello\r\nworld\n' | sed -e '/\r/s///g' | grep -E "^.{6}$")"
<no output>