add psg-function
[small-misc-bash] / psg-function
diff --git a/psg-function b/psg-function
new file mode 100644 (file)
index 0000000..ccdd12a
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/bash
+# Copyright (C) 2016 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+psg () {
+    local x y help
+    help="Usage: psg [--help] GREP_ARGS
+grep ps and output in a nice format"
+    if [[ $1 == --help ]]; then
+        echo "$help"
+        return
+    fi
+    x=$(sudo ps -eF)
+    # final grep is because some commands tend to have a lot of trailing spaces
+    y=$(echo "$x" | grep -P "$@" | grep -o '.*[^ ]')
+    if [[ $y ]]; then
+        echo "$x" | head -n 1
+        echo "$y"
+    fi
+}