从一个多行输出Bash中获取多条线

时间:2015-10-30 08:34:55

标签: bash awk output dumpsys

我有一个带有此输出的命令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

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

2 个答案:

答案 0 :(得分:1)

不清楚你想要什么。你可以使用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'类似于前面的表达式,但会在+符号之后捕获whatere并将其打印在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

答案 1 :(得分:1)

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

这是一个有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