2

I'm trying out the anchor tokens string end $ and string start ^ from regex as a part of a tutorial. How ever only the string start token works. Obviously in the tutorial both tokens work.

Anchor Token $ (not working)

Input file regex16.txt

foo bar baz
bar foo baz
baz foo bar
bar baz foo
foo baz bar
baz bar foo

Command

grep '.*bar$' regex16.txt

Expected Outcome

baz foo bar
foo baz bar

Outcome


Command od -bc regex16.txt

0000000   146 157 157 040 142 141 162 040 142 141 172 015 012 142 141 162
           f   o   o       b   a   r       b   a   z  \r  \n   b   a   r
0000020   040 146 157 157 040 142 141 172 015 012 142 141 172 040 146 157
               f   o   o       b   a   z  \r  \n   b   a   z       f   o
0000040   157 040 142 141 162 015 012 142 141 162 040 142 141 172 040 146
           o       b   a   r  \r  \n   b   a   r       b   a   z       f
0000060   157 157 015 012 146 157 157 040 142 141 172 040 142 141 162 015
           o   o  \r  \n   f   o   o       b   a   z       b   a   r  \r
0000100   012 142 141 172 040 142 141 162 040 146 157 157 015 012        
          \n   b   a   z       b   a   r       f   o   o  \r  \n        
0000116

Other File

Now after manipulating manually another file regex19.txt looks as the following

0000000   154 151 157 156 012 164 151 147 145 162 012 154 145 157 160 141
           l   i   o   n  \n   t   i   g   e   r  \n   l   e   o   p   a
0000020   162 144 012 146 157 170 012 153 141 156 147 141 162 157 157 012
           r   d  \n   f   o   x  \n   k   a   n   g   a   r   o   o  \n
0000040   142 141 164 012 155 157 165 163 145 012 143 165 143 153 157 157
           b   a   t  \n   m   o   u   s   e  \n   c   u   c   k   o   o
0000060   012 144 145 145 162 012                                        
          \n   d   e   e   r  \n       

But this command still doesn't work, even the example regex16.txt now works after manipulating.

grep '^[a-z]{4,6}$' regex19.txt

Anchor Token ^ (working)

Input file regex15.txt

foo bar baz
bar foo baz
baz foo bar
bar baz foo
foo baz bar
baz bar foo

Command

grep '^foo.*' regex15.txt

Expected outcome

foo bar baz
foo baz bar

Outcome

foo bar baz
foo baz bar
  • String start is `^` & string end is `$`. You have stated vice versa in the first line of your question. You code is working just fine for me in Python. – moys Feb 28 '20 at 13:10
  • See my answer - though what you have provided should be working too. – bob dylan Feb 28 '20 at 13:11
  • 3
    Are you sure your line endings are LF? Are they CRLF? – Wiktor Stribiżew Feb 28 '20 at 13:14
  • Show us a line of the output of `od -b regex16.txt`. If there is a carriage return, you will see octal 015. – Quasímodo Feb 28 '20 at 13:15
  • @Quasímodo I pasted is above –  Feb 28 '20 at 13:24
  • 1
    run `dos2unix` on the file and then run it. As @WiktorStribiżew guessed - you have some CRLF characters. – bob dylan Feb 28 '20 at 13:25
  • It does also work here https://regex101.com/r/0BJP6J/1 for JS, PHP, Python and Golang –  Feb 28 '20 at 13:25
  • Other ways to check (other than od) are to use `cat -v regex15.txt` or or `file regex15.txt` my guess is you've copied and pasted this file from a website and it's added those hidden characters without you realising. The reason why ^ works is because it's the start of the line, whereas the $ is the end so includes bar and the ^M char (which you're not looking for) – bob dylan Feb 28 '20 at 13:30
  • One final comment - if you ran `od -bc regex15.txt` you'd see these characters also (rather than just some octal values which are unknown) – bob dylan Feb 28 '20 at 13:32
  • So is `dos2unix` the only way to remove this `^M`? –  Feb 28 '20 at 13:37
  • It's the easiest way - yes. For your example you can vi the file and remove them manually if you want (go to the end delete so the next line is on the same line and return it to a new one). Or google alternatives. – bob dylan Feb 28 '20 at 13:45
  • Does this answer your question? [Why does my tool output overwrite itself and how do I fix it?](https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it) – kvantour Feb 28 '20 at 14:53
  • not really...I now have a new file. I manually removed this `^M` like in the other file and I still have the issues. After using all commands in your recommended question it still doesn't. –  Feb 28 '20 at 15:36

0 Answers0