Score:-1

How to cut part of line?

in flag

I need to cut line in specific moment, I want to display GPU name, but only name, nothing else.

inxi -Gx | grep Device showing:

Device-2: NVIDIA GK107GLM [Quadro K1100M] vendor: Dell driver: nouveau

I want it to show something like this

NVIDIA GK107GLM [Quadro K1100M]

How to cut this to show only name? Is there a way to print range, in this case from word Device to word vendor.

Bodo avatar
pt flag
Please [edit] your question and show the example output of `inxi -Gx | grep Device` and the expected output, [formatted as code](https://stackoverflow.com/editing-help#code). This question is about shell programming / text processing and not specifically related to Ubuntu, so it might better fit on https://stackoverflow.com/
pLumo avatar
in flag
Not working for me
pLumo avatar
in flag
Thanks for your update, however, please check also: "*[...] and the expected output, [formatted as code](https://stackoverflow.com/editing-help#code).*"
Grzegorz Michalak avatar
in flag
It gives output: Graphics: Device-1 Intel 4th Gen Core Processor Integrated Graphics driver: i915
Score:2
cn flag

Tryt to do it this way:

inxi -Gx | sed -n 's/.*Device-.*: \(.*\) vendor.*/\1/p'
Grzegorz Michalak avatar
in flag
Yes, thats it! Thank you very much. Can you describe how this works? This command
Apomelitos avatar
cn flag
Sure. 1. -n param suppresses automatic printing of pattern space(no auto output from sed command) 2. sed is looking for this pattern `.*Device-.*: \(.*\) vendor.*` 3. `/\1/` - sed replaces all the matched line to the first match group (a part of pattern in brackets `(.*\)`) 4. `/p` param in the end prints the current pattern space( print matched lines)
Score:0
bd flag

Something like:

D=$(inxi -Gx | grep Device)

if [[ $D =~ ^Device-2:([[:print:]]*)vendor:([[:print:]]*)driver:([[:print:]]*)$ ]]
then
  echo "Found Device: ${BASH_REMATCH[1]}"
else
  echo "Did not find device"
fi
Grzegorz Michalak avatar
in flag
It gives me error ``` syntax error: "(" unexpected (expecting "then") ```
Wayne Vosberg avatar
bd flag
What version of bash are you using? (`bash --version`)
Grzegorz Michalak avatar
in flag
5.0.3. it should work on newer bash?
Wayne Vosberg avatar
bd flag
It does work with 5.1.4(1). Looking back at the change log regexp matching and BASE_REMATCH *may* be new as of 5.1.
Grzegorz Michalak avatar
in flag
I can't update bash, its says its newest possible :/
bac0n avatar
cn flag
try add `#!/bin/bash`
Grzegorz Michalak avatar
in flag
Its on the first line of script
bac0n avatar
cn flag
maybe you missed semicolon `]]; then`
Wayne Vosberg avatar
bd flag
I just realized I have a RaspberryPI with bash 5.0.3(1). It works there as well. Are you sure you entered the `if [[ ... ]]` line correctly? If so, try adding `set -x` to the beginning of the script - the output then may provide a clue.
Score:0
cn flag
inxi -Gx | grep -oi nv.*]
inxi -Gx | awk '/Device/{print $2,$3,$4,$5}'
Score:0
in flag

inxi -Gx | grep Device | cut -d ':' -f 2 | sed 's/ vendor//'

This cuts the output into fields using ":" as a delimiter, then it gives you the second field. Use sed then to strip the specific word off the end.

If you know the length then you can cut a range using cut, see man cut for details.

in flag
I think, that just using `-G` will strip the vendor part of the string?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.