1

Im running Mac and i wanted to find vendor ID, Product ID, Serial Number and if possible Mount Volume of a pendrive. And ended up with system_profiler SPUSBDataType, which displays whole bunch of list of various devices as paragraphs,.. I was looking forward to get only some details(vendor ID, Product ID, Serial Number) of pendrive. I tried grep, but only either one of the detail was able to get. the command i gave was

$ system_profiler SPUSBDataType | grep -i "Vendor ID:"

What should be the grep command (preferably a single line command) to get only those information of a particular device.

arvindh
  • 739
  • 4
  • 12
  • 28

1 Answers1

2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • im getting an erroy saying `awk: syntax error at source line 3 context is /^$/ >>> p= <<< 0 awk: bailing out at source line 3` – arvindh Apr 06 '15 at 06:31
  • Sorry about that; added the missing braces. – tripleee Apr 06 '15 at 06:35
  • Nice explanation, but it doesn't work to me. I replace to my drive name and returns nothing. – Junix Jun 21 '16 at 21:23
  • @Junix This wasn't a stellar question in the first place. If you can post a new question with sample input and output, and perhaps a link back here for context, we might be able to look at your problem, and you'll get more people looking at it (only myself and the OP will see comments here, basically). – tripleee Jun 22 '16 at 02:48