开发者

How to get the first numbers with SED linux

开发者 https://www.devze.com 2022-12-23 07:31 出处:网络
\"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?

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


$ 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


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

sed 's/ .*//'


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


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


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?


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消