Wednesday, 15 July 2015

shell - How to use grep to first digit of matching line -



shell - How to use grep to first digit of matching line -

i find only first digit of matching line. know first digit can 1 4

for illustration file

2 eggs 1 bacon 5 breadstuff

grep -i eggs | grep ^[14]** or **grep -i eggs | grep ^#

doesnt work.... why not?

the expected result digit 2

you want -o alternative grep output only matching text (not whole line), , regex [1-4] match 1 4 (as opposed [14] matches either 1 or 4)

grep -o '^[1-4]'

to wrap grep pipeline single command, can utilize if have gnu grep:

grep -iop '^[1-4](?=\s+eggs)'

shell command-line grep

No comments:

Post a Comment