-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.

Matt
  • 74,352
  • 26
  • 153
  • 180
  • 2
    post your input and expected output – amdixon Oct 30 '15 at 08: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). – Tom Fenech Oct 30 '15 at 09: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. – tripleee Oct 30 '15 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 :/ –  Nov 04 '15 at 11:41
  • Works for me, in the sense that I get the output I expect: http://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.) – tripleee Nov 04 '15 at 11:57
  • @tripleee I'd like to get the line in whole, so I can simply assign it as a variable and test the value later. That why I thought about using for loops, so I would have something like this: `for i in `dumpsys power`; do #somehow get the lines I need# $i #This would assign, for example, mLastWakeTime as 134887327# done` –  Nov 04 '15 at 14:22
  • Then later I could test the value with a `if` command and do stuff based on that –  Nov 04 '15 at 14:28
  • WIth `print` instead of `print $2` you get the whole line. Often, designs which require variables to be named the same in your code and in an untrusted source is an antipattern, though. If Bash arrays are not out of the question, maybe use them instead. – tripleee Nov 04 '15 at 14:37
  • possible duplicate of http://stackoverflow.com/questions/1952404/linux-bash-multiple-variable-assignment – tripleee Nov 04 '15 at 14:44

2 Answers2

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
estani
  • 24,254
  • 2
  • 93
  • 76
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
hogren
  • 103
  • 6
  • This won't work as the output can vary from device to device, but thanks for the answer anyways :D –  Nov 04 '15 at 11:44