Skip to main content
added 480 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep --color=no -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x*. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:space:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

* Note that, as explained by @dave_thompson-085, this only happens because I have my grep aliased to grep --color=auto which means that color codes are printed around the x, and that's what is causing the x to be overwritten as the \r causes the terminal to move back the cursor, so the x is then overwritten by the non-printing color escape codes.

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:space:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep --color=no -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x*. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:space:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

* Note that, as explained by @dave_thompson-085, this only happens because I have my grep aliased to grep --color=auto which means that color codes are printed around the x, and that's what is causing the x to be overwritten as the \r causes the terminal to move back the cursor, so the x is then overwritten by the non-printing color escape codes.

edited body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:blankspace:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:blank:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:space:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ $ grep -P 'x\s*$' file

xx
$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006
$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:blank:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006
$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx
$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ $ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:blank:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx

If you are using GNU grep, you can use the \s symbol from PCRE which matches any whitespace, so \s* will match 0 or more whitespace characters:

$ printf 'x\r\nxx\n' > file
$ grep -P 'x\s*$' file

xx

Note that what looks like an empty line isn't actually empty, it's the \r which is causing the terminal to move back and overwrite the x. You can see it in action with od:

$ grep -P 'x\s*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

If you don't have GNU grep, you can use the POSIX character class [:blank:] in the same way:

$ grep 'x[[:space:]]*$' file | od -c
0000000   x  \r  \n   x   x  \n
0000006

Removing the \r is also easy with standard tools like tr or sed:

$ tr -d '\r' < file | grep 'x$'
$ tr -d '\r' < file | grep 'x$'
x
xx
$ sed 's/\r//' file | grep 'x$'
x
xx
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718
Loading