SHARE
TWEET

formatting "pacmd list-sink-inputs" using awk

alfonz19 Jan 26th, 2020 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cat exampleOf2Inputs
  2. 2 sink input(s) available.
  3.     index: 144
  4.     driver: <protocol-native.c>
  5.     flags:
  6.     state: RUNNING
  7.     sink: 4 <alsa_output.pci-0000_05_00.0.analog-stereo>
  8.     volume: front-left: 15728 /  24% / -37.19 dB,   front-right: 15728 /  24% / -37.19 dB
  9.             balance 0.00
  10.     muted: no
  11.     current latency: 70.48 ms
  12.     requested latency: 210.00 ms
  13.     sample spec: float32le 2ch 44100Hz
  14.     channel map: front-left,front-right
  15.                  Stereo
  16.     resample method: copy
  17.     module: 13
  18.     client: 245 <MPlayer>
  19.     properties:
  20.         media.name = "UNREAL! Tetris Theme on Violin and Guitar-TnDIRr9C83w.webm"
  21.         application.name = "MPlayer"
  22.         native-protocol.peer = "UNIX socket client"
  23.         native-protocol.version = "32"
  24.         application.process.id = "1543"
  25.         application.process.user = "mmucha"
  26.         application.process.host = "vbDesktop"
  27.         application.process.binary = "mplayer"
  28.         application.language = "C"
  29.         window.x11.display = ":0"
  30.         application.process.machine_id = "720184179caa46f0a3ce25156642f7a0"
  31.         application.process.session_id = "2"
  32.         module-stream-restore.id = "sink-input-by-application-name:MPlayer"
  33.     index: 145
  34.     driver: <protocol-native.c>
  35.     flags:
  36.     state: RUNNING
  37.     sink: 4 <alsa_output.pci-0000_05_00.0.analog-stereo>
  38.     volume: front-left: 24903 /  38% / -25.21 dB,   front-right: 24903 /  38% / -25.21 dB
  39.             balance 0.00
  40.     muted: no
  41.     current latency: 70.50 ms
  42.     requested latency: 210.00 ms
  43.     sample spec: float32le 2ch 48000Hz
  44.     channel map: front-left,front-right
  45.                  Stereo
  46.     resample method: speex-float-1
  47.     module: 13
  48.     client: 251 <MPlayer>
  49.     properties:
  50.         media.name = "Trombone Shorty At Age 13 - 2nd Line-k9YUi3UhEPQ.webm"
  51.         application.name = "MPlayer"
  52.         native-protocol.peer = "UNIX socket client"
  53.         native-protocol.version = "32"
  54.         application.process.id = "2831"
  55.         application.process.user = "mmucha"
  56.         application.process.host = "vbDesktop"
  57.         application.process.binary = "mplayer"
  58.         application.language = "C"
  59.         window.x11.display = ":0"
  60.         application.process.machine_id = "720184179caa46f0a3ce25156642f7a0"
  61.         application.process.session_id = "2"
  62.         module-stream-restore.id = "sink-input-by-application-name:MPlayer"
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. cat exampleOf2Inputs | awk '
  70.     BEGIN { print "[" }
  71.     function print_record() {
  72.         if (count++) {
  73.             print "  {"
  74.             printf "    %s,\n", print_number("pid")
  75.             printf "    %s,\n", print_number("index")
  76.             printf "    %s,\n", print_string("appname")
  77.             printf "    %s\n",  print_string("medianame")
  78.             print "  },"
  79.         }
  80.         delete record
  81.     }
  82.     function print_number(key) { return sprintf("\"%s\": %d", key, record[key]) }
  83.     function print_string(key) { return sprintf("\"%s\": \"%s\"", key, record[key]) }
  84.     function get_quoted_value() {
  85.         if (match($0, /[^"]+"$/)) {
  86.             return substr($0, RSTART, RLENGTH-1)
  87.         else
  88.             return "?"
  89.     }
  90.     $1 == "index:" { print_record(); record["index"] = $2 }
  91.     $1 == "application.process.id" { record["pid"]       = get_quoted_value() }
  92.     $1 == "application.name"       { record["appname"]   = get_quoted_value() }
  93.     $1 == "media.name"             { record["medianame"] = get_quoted_value() }
  94.     END { print_record(); print "]" }
  95. ' |    tac | awk '/},$/ && !seen++ {sub(/,$/,"")} 1' | tac
  96. awk: cmd. line:19:         else
  97. awk: cmd. line:19:         ^ syntax error
  98. awk: cmd. line:22:     $1 == "index:" { print_record(); record["index"] = $2 }
  99. awk: cmd. line:22:                    ^ syntax error
  100. awk: cmd. line:23:     $1 == "application.process.id" { record["pid"]       = get_quoted_value() }
  101. awk: cmd. line:23:                                    ^ syntax error
  102. awk: cmd. line:24:     $1 == "application.name"       { record["appname"]   = get_quoted_value() }
  103. awk: cmd. line:24:                                    ^ syntax error
  104. awk: cmd. line:25:     $1 == "media.name"             { record["medianame"] = get_quoted_value() }
  105. awk: cmd. line:25:                                    ^ syntax error
  106. awk: cmd. line:26:     END { print_record(); print "]" }
  107. awk: cmd. line:26:     ^ syntax error
  108. awk: cmd. line:26:     END { print_record(); print "]" }
  109. awk: cmd. line:26:                                      ^ unexpected newline or end of string
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top