更新时间:2023-02-08 19:40:33
grep
is completely line-oriented; so no, it cannot do paragraph-oriented operations in general. But you could turn to Awk, which is excellent for this type of task.
Without access to sample output, this is speculative, but something like
system_profiler SPUSBDataType |
awk '/this is the drive you want/ { p=1 }
/vendor ID|Product ID|Serial Number/ && p
/^$/ { p=0 }'
This is assuming that the regular expression this is the drive you want
matches the first line of output for the particular drive you are interested in, and that output for individual drives is separated by empty lines (the regular expression ^$
matches that). Now the variable p
will be true between those two lines; then we print when we see a match on the middlemost regular expression.