-2

I have a command dumpsys power with this output:

    POWER MANAGER (dumpsys power)
    Power Manager State: mDirty=0x0
    mWakefulness=Awake #
    mWakefulnessChanging=false
    mIsPowered=false
    mPlugType=0
    mBatteryLevel=67 #
    mBatteryLevelWhenDreamStarted=0
    mDockState=0
    mStayOn=false #
    mProximityPositive=false
    mBootCompleted=true #
    mSystemReady=true #
    mHalAutoSuspendModeEnabled=false
    mHalInteractiveModeEnabled=true
    mWakeLockSummary=0x0
    mUserActivitySummary=0x1
    mRequestWaitForNegativeProximity=false
    mSandmanScheduled=false
    mSandmanSummoned=false
    mLowPowerModeEnabled=false #
    mBatteryLevelLow=false #
    mLastWakeTime=134887327 (59454 ms ago) #
    mLastSleepTime=134881809 (64972 ms ago) #
    mLastUserActivityTime=134946670 (111 ms ago)
mLastUserActivityTimeNoChangeLights=134794061 (152720 ms ago)
mLastInteractivePowerHintTime=134946670 (111 ms ago)
mLastScreenBrightnessBoostTime=0 (134946781 ms ago)
mScreenBrightnessBoostInProgress=false
mDisplayReady=true #
mHoldingWakeLockSuspendBlocker=false
mHoldingDisplaySuspendBlocker=true
    Settings and Configuration:
mDecoupleHalAutoSuspendModeFromDisplayConfig=false
mDecoupleHalInteractiveModeFromDisplayConfig=true
mWakeUpWhenPluggedOrUnpluggedConfig=true
mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig=false
mTheaterModeEnabled=false
mSuspendWhenScreenOffDueToProximityConfig=false
mDreamsSupportedConfig=true
mDreamsEnabledByDefaultConfig=true
mDreamsActivatedOnSleepByDefaultConfig=false
mDreamsActivatedOnDockByDefaultConfig=true
mDreamsEnabledOnBatteryConfig=false
mDreamsBatteryLevelMinimumWhenPoweredConfig=-1
mDreamsBatteryLevelMinimumWhenNotPoweredConfig=15
mDreamsBatteryLevelDrainCutoffConfig=5
mDreamsEnabledSetting=false
mDreamsActivateOnSleepSetting=false
mDreamsActivateOnDockSetting=true
mDozeAfterScreenOffConfig=true
mLowPowerModeSetting=false
mAutoLowPowerModeConfigured=false
mAutoLowPowerModeSnoozing=false
mMinimumScreenOffTimeoutConfig=10000
mMaximumScreenDimDurationConfig=7000
mMaximumScreenDimRatioConfig=0.20000005
mScreenOffTimeoutSetting=60000 #
mSleepTimeoutSetting=-1
mMaximumScreenOffTimeoutFromDeviceAdmin=2147483647 (enforced=false)
mStayOnWhilePluggedInSetting=0
mScreenBrightnessSetting=102
mScreenAutoBrightnessAdjustmentSetting=-1.0
mScreenBrightnessModeSetting=1
mScreenBrightnessOverrideFromWindowManager=-1
mUserActivityTimeoutOverrideFromWindowManager=-1
mTemporaryScreenBrightnessSettingOverride=-1
mTemporaryScreenAutoBrightnessAdjustmentSettingOverride=NaN
mDozeScreenStateOverrideFromDreamManager=0
mDozeScreenBrightnessOverrideFromDreamManager=-1
mScreenBrightnessSettingMinimum=10
mScreenBrightnessSettingMaximum=255
mScreenBrightnessSettingDefault=102
Sleep timeout: -1 ms
Screen off timeout: 60000 ms
Screen dim duration: 7000 ms
Wake Locks: size=0 Suspend Blockers: size=4
PowerManagerService.WakeLocks: ref count=0
PowerManagerService.Display: ref count=1
PowerManagerService.Broadcasts: ref count=0
PowerManagerService.WirelessChargerDetector: ref count=0
Display Power: state=ON #

I want to get the lines marked with # in a format of:

mScreenOffTimeoutSetting=60000
mDisplayReady=true
***
ScreenOfftimeoutSetting = 60000
DisplayReady = true

The commands output can vary from device to device and some of the lines might not be there or are in a different place. Thus if the searched line isn't there no errors should be generated.

9
  • 2
    post your input and expected output Commented Oct 30, 2015 at 8:44
  • If your output is 50-100 lines long, it'd perhaps be better to make a smaller example (e.g. 10 lines, extracting 2). Commented Oct 30, 2015 at 9:20
  • There is no need for a for loop. awk -F= '$1 ~ /^(mSomeValue|someOther|orThirdEtc)$/ { print $2 }' will extract the values from the keys in the regex to standard output. Commented Oct 30, 2015 at 10:53
  • I updated the question with my full output from the command. I've tried your awk command, but for some reason it returns blank for me :/ Commented Nov 4, 2015 at 11:41
  • Works for me, in the sense that I get the output I expect: ideone.com/KHxT3j ... Whether that output is actually useful is another question, but your problem statement only leaves us guessing. (If you want to ignore leading whitespace, add something like [ ]* after the first ^ in the regex.) Commented Nov 4, 2015 at 11:57

2 Answers 2

1

It's not clear what you want. Aou can use sed to extract variables form the file and do whatever you want with them. Here's an example:

sed -n -e 's/^mSomeName=\(.*\)/newVariable=\1/p' -e 's/^mOtherName=.*+\(.*\)/newVariable2=\1/p' myFile

Explanation:

  • -n don't output anything per default
  • -e an expression follows. It's required since we have multiple expressions in place
  • s/^mSomeName=\(.*\)/newVariable=\1/p if file starts (^) with mSomeName= capture what follows (\(.*\)), replace the line with newVariable=\1, where \1 is what got captured, and print it out (p)
  • 's/^mOtherName=.+(.)/newVariable2=\1/p' similar to the previous expression but will capture whatere comes after a + sign and print it behind newVariable2

This does something like:

$ sed -n -e 's/^mSomeName=\(.*\)/newVariable=\1/p' -e 's/^mOtherName=.*+\(.*\)/newVariable2=\1/p' <<<$'mSomeName=SomeValue\nmOtherName=OtherValue+Somethingelse'
newVariable=SomeValue
newVariable2=Somethingelse

<<<$'...' is a way of passing a string with linebreaks \n directly to the command in bash. You can replace it with a file. This command just outputs a string, nothing will get changed.

If you need them in bash variables use eval:

$ eval $(sed -n -e 's/^mSomeName=\(.*\)/newVariable=\1/p' -e 's/^mOtherName=.*+\(.*\)/newVariable2=\1/p' <<<$'mSomeName=SomeValue\nmOtherName=OtherValue+Somethingelse')
$ echo newVariable=$newVariable - newVariable2=$newVariable2
newVariable=SomeValue - newVariable2=Somethingelse

eval will execute the string which in this case set the variable values:

$ eval a=1
$ echo $a
1
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to just use Grep command, you can use -A (After) and -B (Before) options and pipes.

This is a exemple with 2 lines.

File test.txt :

test
aieauieaui
test
caieaieaipe

mSomeName=SomeValue
mOtherName=OtherValue+Somethingelse
nothing
blabla
mSomeName=SomeValue2
mOtherName=OtherValue+Somethingelse2

The command to use :

grep -A 1 'mSomeName' test.txt |grep -B 1 'mOtherName'

The output :

mSomeName=SomeValue
mOtherName=OtherValue+Somethingelse
--
mSomeName=SomeValue2
mOtherName=OtherValue+Somethingelse2

1 Comment

This won't work as the output can vary from device to device, but thanks for the answer anyways :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.