Author Topic: [SOLVED] Extracting Required info from a List ...... help needed please  (Read 1351 times)

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
I have been playing about with Pulse Audio of late, and it has commands which can be used to get info and control PA.

So I can generate a list of what is active in PA using the command

pactl list

That gives an output of many things, but what I am interested in is extracting information about the loaded modules. This is what part of the list looks like ...

Code: [Select]
Module #25
Name: module-switch-on-port-available
Argument:
Usage counter: n/a
Properties:


Module #29
Name: module-null-sink
Argument: sink_name=MySink
Usage counter: 2
Properties:
module.author = "Lennart Poettering"
module.description = "Clocked NULL sink"
module.version = "2.0-1pclos2012"

Module #30
Name: module-loopback
Argument: sink=MySink
Usage counter: n/a
Properties:
module.author = "Pierre-Louis Bossart"
module.description = "Loopback from source to sink"
module.version = "2.0-1pclos2012"

Module #31
Name: module-loopback
Argument: sink=MySink
Usage counter: n/a
Properties:
module.author = "Pierre-Louis Bossart"
module.description = "Loopback from source to sink"
module.version = "2.0-1pclos2012"

As can be seen from the above, there are two 'Name: module-loopback' entries.

Although I can load the modules by name, I can only unload them again using the ID number. So I need, I believe, to search the list for each instance of the module name and then extract the module ID # from the previous line, so that number can be used to unload the module.

I hope I am making some sense  :(

As an example

Quote
Module #31
   Name: module-loopback
   Argument: sink=MySink
   Usage counter: n/a
   Properties:
      module.author = "Pierre-Louis Bossart"
      module.description = "Loopback from source to sink"
      module.version = "2.0-1pclos2012"

I want to search for Name: module-loopback and store 31

Alternatively I suppose I could search for all module numbers (could be quite a few) then look for a matching string for the module name I want, and store the last number encountered.

I would appreciate some help with this ....  what might be the best simplest approach etc.

Sorry should have mentioned .......  BASH

Any help appreciated, thank you  ;)


« Last Edit: June 01, 2012, 09:44:46 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Extracting Required info from a List ...... help needed please
« Reply #1 on: June 01, 2012, 08:45:09 AM »
something like that?
Code: [Select]
pactl list | grep -B 1 'Name: module-loopback'
PS

If you want to get only numbers then
Code: [Select]
pactl list | grep -B 1 'Name: module-loopback' | grep 'Module #' | cut -d '#' -f 2-
« Last Edit: June 01, 2012, 08:48:27 AM by sir_herrbatka »

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
Re: Extracting Required info from a List ...... help needed please
« Reply #2 on: June 01, 2012, 09:11:22 AM »

If you want to get only numbers then
Code: [Select]
pactl list | grep -B 1 'Name: module-loopback' | grep 'Module #' | cut -d '#' -f 2-

Thank you.

That is better than my attempt here ...

Code: [Select]
pactl list | grep -B 1 -i "Name: module-loopback" | cut -d "N" -f1 | cut -d "-" -f2 | cut -d "#" -f2
... I was trying to work through it. Obviously I had not trimmed the one-liner as much as possible.

Now I have to get each of the returned numbers into a separate variable ...... like $MODULE1, $MODULE2,$MODULE3, etc

MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
Re: Extracting Required info from a List ...... help needed please
« Reply #3 on: June 01, 2012, 09:30:34 AM »
I am looking at something like this ...

Code: [Select]
MyNum=1
for i in $(pactl list | grep -B 1 'Name: module-loopback' | grep 'Module #' | cut -d '#' -f 2-)
do
echo MODULE"$MyNum"=$i
let MyNum=$MyNum+1
done

MODULE1=33
MODULE2=34
MODULE3=35
MODULE4=36

I feel sure there can be improvements made, so please advise  ;)

MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Extracting Required info from a List ...... help needed please
« Reply #4 on: June 01, 2012, 09:39:52 AM »
Quote
I feel sure there can be improvements made, so please advise  ;)
For me it looks ok and certainly good enough. No need to polish everything ;-)

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
Re: Extracting Required info from a List ...... help needed please
« Reply #5 on: June 01, 2012, 09:44:28 AM »
Quote
I feel sure there can be improvements made, so please advise  ;)
For me it looks ok and certainly good enough. No need to polish everything ;-)

Thanks for the help  ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
happy to help

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
This explains what was behind the questions  ;)

http://www.pclinuxos.com/forum/index.php/topic,105968.0.html
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
BTW You may want to learn awk. It can use two delimiters at once so it's supreme to cut.

Offline Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10647
  • MLUs Forever!
BTW You may want to learn awk. It can use two delimiters at once so it's supreme to cut.

:D  Too lazy  :D

Looked at both awk and sed a couple of years ago, and decided there was too much effort involved  ;D
« Last Edit: June 01, 2012, 02:11:41 PM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
hehe, same here. Sed is just scary but awk is also useful.