I have this:
echo 12345 | grep -o '[[:digit:]]\{1,4\}'
Which gives this:
1234
5
I understand whats happening. How do I stop grep from trying to continue matching after 1 successful match?
How do I get only
1234
The uniq command reports or omits repeated lines and by passing it the -u argument we tell it to report only unique lines. Used together like this, the command will sort data. txt lexicographically by each line, find the unique line and print it back in the terminal for you.
Do you want grep to stop matching or do you only care about the first match. You could use head
if the later is true...
`grep stuff | head -n 1`
Grep is a line based util so the -m 1
flag tells grep to stop after it matches the first line which when combined with head is pretty good in practice.
You need to do the grouping: \(...\)
followed by the exact number of occurrence: \{<n>\}
to do the job:
maci:~ san$ echo 12345 | grep -o '\([[:digit:]]\)\{4\}'
1234
Hope it helps. Cheers!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With