Score:1

'.*' regex properly not functioning in ubuntu of wsl

ci flag

There was a file with the following text:

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

I needed to get the 1st one foo bar baz and the 5th one foo baz bar as the output to the console.

Hence I tried the command grep '^foo.*' file But the output is not coming as expected. I tried the same thing inside the Ubuntu terminal installed in the virtual box and it is appropriately functioning there.

Two images are attached: Run in WSL Ubuntu Run in Virtual Box Ubuntu

terdon avatar
cn flag
Please [don't post images of text](https://meta.askubuntu.com/q/8713/85695). Instead, copy/paste the text into your question and use the [formatting tools](https://askubuntu.com/help/formatting) to format it as code.
terdon avatar
cn flag
Also, is this the same file or two different files with the same content? My first guess is that you have some non-printing characters in the file you used in your WSL instance. What is the output of `awk '$1~/foo/' regex15.txt | head -n1 | od -c`? Please add that to your question.
Score:2
cn flag

The problem is that you have edited the file in Windows which means that it now has Windows style line endings. This means \r\n instead of \n. The \r is called a carriage return and is the command to move back to the start of the line, which it does and results in overwriting the output. You can easily reproduce the behavior if you add the \r yourself:

$ printf 'foo bar baz\r\n' | grep '^foo.*'

$ printf 'foo bar baz\n' | grep '^foo.*'
foo bar baz

You can also see that the output is there if you pass it through something like od -c or cat -v which will show non-printing characters:

$ printf 'foo bar baz\r\n' | grep '^foo.*' | cat -v
foo bar baz^M

The ^M is the \r. So, the solution is to never edit files in Windows if you want to use them in Linux, or to use a proper editor that can let you choose what line ending style to use (apparently, Notepad++ can do this) or to fix the file on the Linux side with:

dos2unix file

Or

sed -i 's/\r//' file

For more information on line endings, see https://en.wikipedia.org/wiki/Newline.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.