1

Try this: download https://www.mathworks.com/matlabcentral/fileexchange/19-delta-sigma-toolbox

In the unzipped folder, I get the following results:

ack --no-heading --no-break --matlab dsexample

Contents.m:56:%   dsexample1      - Discrete-time lowpass/bandpass/quadrature modulator.
Contents.m:57:%   dsexample2      - Continuous-time lowpass modulator.
dsexample1(dsm, LiveDemo); 
fprintf(1,'Done.\n');
adc.sys_cs = sys_cs;

grep -nH -R --include="*.m" dsexample

Contents.m:56:%   dsexample1      - Discrete-time lowpass/bandpass/quadrature modulator.
Contents.m:57:%   dsexample2      - Continuous-time lowpass modulator.
dsexample1(dsm, LiveDemo); d center frequency larger Hinfation Script
fprintf(1,'Done.\n');c = c;formed.s of finite op-amp gain and capacitorased;;n for the input.
adc.sys_cs = sys_cs;snr;seed with CT simulations tora states used in the d-t model_amp); Response');

What's going on ?

[Edit for clarification]: Why is there no file name, no line number on the 3rd line result ? Why results on the 4th and 5th line do not even contain dsexample ?

NB: using ack 3.40 and grep 2.16

DaveC
  • 115
  • 8
  • 1
    [so] is for programming questions, not questions about using or configuring Unix and its utilities. [unix.se] or [su] would be better places for questions like this. – Barmar Feb 22 '21 at 22:17
  • What's the problem? Is it because you're getting multiple lines of output for a match? – Barmar Feb 22 '21 at 22:18
  • 2
    My guess is there's a CR line separator and something is converting it to newline. – Barmar Feb 22 '21 at 22:20
  • What should the output be? – ti7 Feb 22 '21 at 23:08
  • I appreciate that needing an account to download the files is not convenient... But at the same time I didn't want to change them at all, since I don't know what's peculiar about these files that confuses ack & grep. I've added an alternative download location [here](https://1drv.ms/u/s!AgEAl1i8LRkyoSwPqGS-EX8jBuux?e=c7ufvl) – DaveC Feb 22 '21 at 23:17
  • I don't know what the output should be. All I know is that the result on the 3rd line does contain `dsexample` as expected but for some reason there is no file name, no line number. Results on the 4th and 5th line have no file name and no line number either, but they just don't contain `dsexample` at all..... I will edit my OP to make it more explicit. – DaveC Feb 22 '21 at 23:18
  • Ok right. Seeing the `grep ...| od -c` output would have confirmed the issue, but of course it would be rather long, and I was wrong to ask about `Contents.m` anyway. – ilkkachu Feb 23 '21 at 10:29

3 Answers3

2

I do not deserve any credits for this answer - It is all about line endings.

I have known for years about Windows line endings (CR-LF) and Linux line endings (LF only), but I had never heard of Legacy MAC line endings (CR only)... The latter really upsets ack, grep, and I'm sure lots of other tools.

dos2unix and unix2dos have no effect on files with Legacy MAC format - But after using this nifty little endline tool, I could eventually bring some consistency to the source files:

endlines : 129 files converted from :
              - 23 Legacy Mac (CR)
              - 105 Unix (LF)
              - 1 Windows (CR-LF)

Now, ack and grep are much happier.

DaveC
  • 115
  • 8
  • Bare carriage return used to be fairly common in the old days of many different OSes; Commodore 8-bits used the same convention. But it pretty much vanished with the dominance of C and UNIX; pre-X MacOS was one of the last holdouts. Any such single-character line-ending sentinel requires some conversion logic in the driver to output both characters when the file is sent to a screen or printer; CP/M simplified the code at the expense of requiring both characters to be actually stored at the end of every line in every file. DOS and then Windows inherited that design decision. – Mark Reed Feb 23 '21 at 03:41
1

Let's see what files contain dsexample, grep -l doesn't print the contents, just file names:

$ grep -l dsexample *
Contents.m
demoLPandBP.m
dsexample1.m
dsexample2.m

Ok, then, file shows that they have CR line terminators. (It would say "CRLF line terminators" for Windows files.)

$ file Contents.m demoLPandBP.m dsexample*
Contents.m:    ASCII text
demoLPandBP.m: ASCII text, with CR line terminators
dsexample1.m:  ASCII text, with CR line terminators
dsexample2.m:  ASCII text, with CR line terminators

Unlike what I commented about before, Contents.m is fine. Let's look at another one, how it prints:

$ grep dsexample demoLPandBP.m 
dsexample1(dsm, LiveDemo); d center frequency larger Hinf

The output from grep is actually the whole file, since grep doesn't consider the plain CR as breaking a line -- the whole file is just one line. If we change CRs to LFs, we see it better, or can just count the lines:

$ grep dsexample demoLPandBP.m | tr '\r' '\n' | wc -l
51

These are the longest lines there, in order:

%% 5th-order lowpass with optimized zeros and larger Hinf
dsm.f0 = 1/6;   % Normalized center frequency
dsexample1(dsm, LiveDemo); 

With a CR in the end of each, the cursor moves back to the start of the line, partially overwriting the previous output, so you get:

dsexample1(dsm, LiveDemo); d center frequency larger Hinf

(There's a space after the semicolon on that line, so the e gets overwritten too. I checked.)

Someone said dos2unix can't deal with that, and well, they're not DOS or Windows files anyway so why should it. You could do something like this, though, in Bash:

for f in *.m; do
    if [[ $(file "$f") = *"ASCII text, with CR line terminators" ]]; then
        tr '\r' '\n' < "$f" > tmptmptmp &&
        mv tmptmptmp "$f"
    fi
done

I think it was just the .m files that had the issue, hence the *.m in the loop. There was at least one PDF file there, and we don't want to break that. Though with the check on file there, it should be safe even if you just run the loop on *.

ilkkachu
  • 6,221
  • 16
  • 30
0

It looks like both ack and grep are getting confused by the line endings in the files. Run file *.m on your files. You'll see that some files have proper linefeeds, and some have CR line terminators.

If you clean up your line endings, things should be OK.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • You are right that all files do not use the same convention and that there is a mixture of line endings... Note that these are not my files... just some open source stuff available to anyone, so this is out of my control Having said that - I get the very same results from grep and ack both on Linux and on Windows (grep from [ezwinports](https://sourceforge.net/projects/ezwinports/files/)), so line endings may not be the root cause.... I have tried to convert the files to a consistent representation but still getting the same results... – DaveC Feb 23 '21 at 00:42
  • I'm pretty sure you'll find that it's the line-endings. If you can track it down to something else that is a bug in ack, please report it at https://github.com/beyondgrep/ack3 (I'm the author). – Andy Lester Feb 23 '21 at 01:12
  • I will investigate a little more and let you know ! I don't think it's a bug in ack, since grep behaves pretty much the very same... so it's likely to be something in the source files.... I've found that the 3rd, 4th and 5th lines of the output are from matches located in two files: `dsexample1.m` and `dsexample2.m` - So you can delete everything but these 2 files to investigate. – DaveC Feb 23 '21 at 01:41
  • I can't share the results restricted to searching those 2 files with decent formatting, so you'll have to trust me.... But searching either after `dos2unix` or after `unix2dos` on both files yields exactly the same results. – DaveC Feb 23 '21 at 01:42
  • I have found that if I run ack with `--heading` instead of `--no-heading` as shown in the OP then at least the filename where the match is found is printed properly. Still no line number though. The 3rd match actually appears to be from `demoLPandBP.m` - I thought it was from `dsexample1.m` but I was wrong. This file actually contains 7 occurrences (!) of `dsexample` – DaveC Feb 23 '21 at 01:54
  • If you're still having a problem, please submit it as an issue at https://github.com/beyondgrep/ack3/issues so I or someone else on the team can pursue it further. – Andy Lester Feb 23 '21 at 14:18