I have a command (logcat) which outputs a long log like this:
07-27 09:26:22.416 17574 17886 D SurfaceControl: Excessive delay in setPowerMode()
07-27 09:26:22.421 17574 17599 I PowerManagerService: Sleeping (uid 1000)...
07-27 09:26:22.427 17489 17527 D audio_hw_primary: adev_set_parameters: enter: screen_state=off
The format is: [Date] [Time] [Process ID] [Thread ID] [Log Type] [Text]
Instead of the process id, I would like to see the name of the process (or package to be more accurate).
The name of a package can be retrieved by process ID using:
echo "$PKG_PID_LIST" | grep -w $PID | cut -d ' ' -f1
E.g.:
$ echo "$PKG_PID_LIST" | grep -w 17489 | cut -d ' ' -f1
org.lineageos.audiofx
If you're curious, yes, I'm on Android and I'm creating the list using:
PKG_PID_LIST=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
In the end the output should look something like this:
07-27 09:26:22.416 com.android.systemui 17886 D SurfaceControl: Excessive delay in setPowerMode()
07-27 09:26:22.421 com.android.systemui 17599 I PowerManagerService: Sleeping (uid 1000)...
07-27 09:26:22.427 org.lineageos.audiofx 17527 D audio_hw_primary: adev_set_parameters: enter: screen_state=off
edit: Example output of $PKG_PID_LIST:
$ echo "$PKG_PID_LIST"
com.android.deskclock 18937
com.android.systemui 17574
net.osmand.srtmPlugin.paid
com.android.bluetoothmidiservice
org.lineageos.audiofx 17489
(Packages that are not running don't have a PID. There is always a space behind the package name.)