Yannis Assael
Yannis Assael

Reputation: 1119

How to get the first numbers with SED linux

"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!

Upvotes: 2

Views: 8632

Answers (8)

WisdomFusion
WisdomFusion

Reputation: 510

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

Upvotes: 0

Idelic
Idelic

Reputation: 15582

X="00:02.0 VGA compatible ..."
set $X; echo $1

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360035

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

sed 's/ .*//'

Upvotes: 5

Eric Eijkelenboom
Eric Eijkelenboom

Reputation: 7021

echo '00:02.0 VGA compatible bla bla bla' | sed -e 's/\(^[0-9:\.]*\).*/\1/'

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342353

$ 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

Upvotes: 5

mdeous
mdeous

Reputation: 18029

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?

Upvotes: 2

Chris Johnsen
Chris Johnsen

Reputation: 224651

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.

Upvotes: 1

Gregory Pakosz
Gregory Pakosz

Reputation: 70204

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

Upvotes: 2

Related Questions