Timeline for What is the need for writing `\n` in this grep
Current License: CC BY-SA 4.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 11, 2023 at 13:17 | comment | added | Dhruv | Even echo $'a\tb\nc\td' | grep -z 'c[^;]*d' | less prints all the four characters, with tabs, newline and ^@(at the end). | |
| Feb 11, 2023 at 13:13 | comment | added | Dhruv | No I used -z flag so it doesn't treat the input as two separate lines. eg - echo $'a\nb' | grep -z '\n' | less the output is a b ^@ (each in a new line). ^@ represents null character. The input is treated as null terminated string. So \n matches a part of line and grep prints the matching line (the whole line itself). | |
| Feb 11, 2023 at 13:02 | comment | added | White Owl | @Dhruv No bugs. Your sample string is treated as two separate lines by grep : "a\tb" and "c\td". So it can find pattern which includes "c" and "d", but letters "a" and "d" are on different lines - so nothing is found. | |
| Feb 11, 2023 at 12:06 | comment | added | Dhruv | I think it's a bug related to \n as pointed out by @Stéphane Chazelas. Because I tried a similar thing for \t and it worked. eg - echo $'a\tb\nc\td' | grep -zEo 'c[^;]*d' outputs c[8 spaces]d but echo $'a\tb\nc\td' | grep -zEo 'a[^;]*d' outputs nothing. | |
| Feb 11, 2023 at 2:39 | comment | added | White Owl | @Dhruv Yes, that is possible. There are several regexp variations. With grep you can choose one of the four major regexp dialects (keys -E, -F, -G, -P). And even then, by using different versions of grep you can encounter differences in regexp processing. Your grep in "extended" mode (you have -E key in the command) does not include \n in \W. The online regexp checker most likely using javascript or Perl versions - they do include it. Run your command with -P instead of -E and see the difference. | |
| Feb 10, 2023 at 15:41 | comment | added | Dhruv | I tried my expression on online regex, it works fine. \W means any character other than [a-zA-Z0-9_] which includes \n as well. | |
| Feb 10, 2023 at 13:52 | history | answered | White Owl | CC BY-SA 4.0 |