0

I am trying to look for patterns in a file. The looks like this:

aaa;
bbb;

If I try the following it does not stop:

cat test | tr -d '\n' | ack -1 'aa.*;'
aaa;bbb;

Is there a way to stop with aaa;?

Istvan
  • 7,500
  • 9
  • 59
  • 109
  • Please add your desired output for that sample input to your question. – Cyrus May 22 '19 at 17:48
  • It is added, maybe you need more highlighting? – Istvan May 22 '19 at 17:49
  • Thanks, I missed that. – Cyrus May 22 '19 at 17:51
  • This might help: `head -n 1 test` – Cyrus May 22 '19 at 17:52
  • The pattern that I am looking for is not in the first position. It can be in the middle of the file. ACK does not support multi-line pattern matching and it was suggested on other SO answers that removing the new line from the file is the only way to look for patterns that can span over multiple lines. – Istvan May 22 '19 at 17:54
  • You've deleted all linefeeds, so your file is now one line, surely? So if you are expecting it to stop at the next line, that will be the end of file. – Mark Setchell May 22 '19 at 20:47

2 Answers2

1

Your regex is greedy, so it includes all text after the second a until the final semi-colon. If you refine the pattern to NOT be greedy (with a question mark) and use the -o option, you'll get what you expect:

$cat test.txt | tr -d '\n' | ack -o 'aa.*?;' 
aaa;

See more recipes/details here

gregory
  • 10,969
  • 2
  • 30
  • 42
0

This is doing the trick:

 ack -1 -io 'a[^;]+;'
Istvan
  • 7,500
  • 9
  • 59
  • 109