-2

我有一个dumpsys power带有此输出的命令:

    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 #

我想以以下格式获取标有 # 的行:

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

命令输出可能因设备而异,并且某些行可能不存在或位于不同的位置。因此,如果搜索到的行不存在,则不应生成错误。

4

2 回答 2

1

不清楚你想要什么。Aou 可以使用 sed 从文件中提取变量,然后对它们做任何你想做的事情。这是一个例子:

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

解释:

  • -n默认情况下不输出任何内容
  • -e下面是一个表达式。这是必需的,因为我们有多个表达式
  • s/^mSomeName=\(.*\)/newVariable=\1/p如果文件^mSomeName=捕获后面的内容 ( ) 开头 ( ) \(.*\),则将该行替换为,捕获的内容newVariable=\1在哪里\1,然后打印出来 ( p)
  • 's/^mOtherName=. +(. )/newVariable2=\1/p' 类似于前面的表达式,但会捕获+符号后面的内容并将其打印在后面newVariable2

这会做类似的事情:

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

<<<$'...'是一种将带有换行符的字符串\n直接传递给 bash 中的命令的方法。您可以将其替换为文件。该命令只输出一个字符串,不会改变任何内容。

如果您在 bash 变量中需要它们,请使用 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 将执行在这种情况下设置变量值的字符串:

$ eval a=1
$ echo $a
1
于 2015-10-30T09:50:50.143 回答
1

如果您只想使用 Grep 命令,您可以使用 -A(之后)和 -B(之前)选项和管道。

这是一个有 2 行的示例。

文件 test.txt :

test
aieauieaui
test
caieaieaipe

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

要使用的命令:

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

输出 :

mSomeName=SomeValue
mOtherName=OtherValue+Somethingelse
--
mSomeName=SomeValue2
mOtherName=OtherValue+Somethingelse2
于 2015-10-30T10:17:12.890 回答