Reputation: 79
Requirements: only grep/cut/join/regex.
I have data like this:
798 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
15386 /usr/bin/nautilus --gapplication-service
16051 /usr/bin/zeitgeist-daemon
I want to extract rows data from the number to second ending space, like
798 /usr/bin/dbus-daemon
using only grep/cut/join with or without regex.
I have tried
grep -oe "[^ ][^ ]* *[a-zA-Z\]*$"
but the result isn't as expected.
Upvotes: 1
Views: 51
Reputation: 626896
You may use
# With GNU grep:
grep -oP '^\s*\K\S+\s+\S+' <<< "$s"
# With a POSIX ERE pattern:
grep -oE '[0-9][^ ]* +[^ ]+' <<< "$s"
See the online demo
o
- match output mode (not line)P
- PCRE regex engine is used to parse the patternThe PCRE pattern details:
^
- start of line\s*
- 0+ whitespaces\K
- match reset operator discarding the whole text matched so far\S+
- 1+ non-whitespace chars\s+\S+
- 1+ whitespaces and 1+ non-whitespace chars.The POSIX ERE pattern matches
[0-9]
- a digit[^ ]*
- 0+ chars other than space +
- 1 or more spaces [^ ]+
- 1+ chars other than a space.Upvotes: 1