tags:

views:

97

answers:

8

Hello i have this line... "00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter" how can i get the first numbers until VGA with SED in Bash script? Thanks!

+1  A: 
sed -e 's/\([0-9][0-9]:[0-9][0-9]\.[0-9]\).*/\1/'

will keep the numbers.

In a script you're likely going to pipe the command that returns you the string to sed, like

#!/bin/sh
echo "00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter" | sed -e 's/\([0-9][0-9]:[0-9][0-9]\.[0-9]\).*/\1/'

which gives

00:02.0
Gregory Pakosz
+1  A: 

It looks like you are parsing lspci output. If so, you might want to look into the -m option that should be a bit easier to parse. If you are intent on using sed with the default output format, then you might be able to do what you want like this:

echo '00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter' |
  sed -e 's/\([0-9a-fA-F][0-9a-fA-F]\):\([01][0-9a-fA-F]\)\.\([0-7]\) .*/\1 \2 \3/' |
  while read bus slot func; do 
    echo "bus: $bus; slot: $slot; func: $func"
  done

If you are really only reading one line, you could do it without the while loop, but I included it in case you are actually wanting to parse multiple lines of lspci output.

Chris Johnsen
+1  A: 

imho it would be simplier to use awk instead of sed, using awk for what you want would give this:

echo '00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter' | awk '{print $1}'

much less complicated than using sed, isn't it?

MatToufoutu
+2  A: 
$ s="00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter"
$ echo $s| sed 's/\(.*\)VGA.*/\1/'
00:02.0
$ echo $s| sed 's/\([0-9]\+:[0-9]\+.*\)VGA.*/\1/'
00:02.0
$ echo $s| sed 's/VGA.*//'
00:02.0

or awk

$ echo $s| awk '{print $1}'
00:02.0
ghostdog74
+1  A: 
echo '00:02.0 VGA compatible bla bla bla' | sed -e 's/\(^[0-9:\.]*\).*/\1/'
Eric Eijkelenboom
+1  A: 

This will also work, but it relies on there being a space after the numbers you want.

sed 's/ .*//'
Dennis Williamson
+1  A: 
X="00:02.0 VGA compatible ..."
set $X; echo $1
Idelic
A: 

try, sed -e 's/(^[0-9:.])./\1/' urfile

WisdomFusion
try, <code>cat urfile | sed -e 's/\(^[0-9:\.]*\).*/\1/'</code>
WisdomFusion