简洁的多行子串提取

时间:2017-07-18 05:32:59

标签: android perl

好的,我知道一些shell魔法,但我的脑袋在一天的这个时候不会解析所需的sed并发症,所以我尝试了perl。哈哈,这是一个多变的空间爆炸...但不知怎的,它只是花了我一个小时。 S0Oo。滥用我的shell for REPL尝试从android安装包列表中提取名称和版本..

dumpsys package packages > dump
( set -xv; while perl -ne "$(rlwrap cat)" < dump; do eval printf "=%.s" {1..$(tput cols)}; echo; done; )

我认为这可行:

 dumpsys package packages|perl -ne 'if (/Package \[(.*)\]/g) {print "$1 -- "}; if (/versionName=(.*)/g) {print "$1\n"}'|sort

如何缩短? dumpsys输出如下所示:

  Package [org.fdroid.fdroid.privileged] (64fb46e0):
    userId=10022 gids=[]
    pkg=Package{6501d0a8 org.fdroid.fdroid.privileged}
    codePath=/system/priv-app/FDroidPrivileged.apk
    resourcePath=/system/priv-app/FDroidPrivileged.apk
    nativeLibraryPath=/data/app-lib/FDroidPrivileged
    versionCode=1050 targetSdk=22
    versionName=0.1
    applicationInfo=ApplicationInfo{64f94838 org.fdroid.fdroid.privileged}
    flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
    dataDir=null
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    timeStamp=2016-02-25 08:36:51
    firstInstallTime=2016-02-25 08:36:51
    lastUpdateTime=2016-02-25 08:36:51
    signatures=PackageSignatures{64fb4818 []}
    permissionsFixed=false haveGids=false installStatus=1
    pkgFlags=[ SYSTEM PRIVILEGED ]
    User 0:  installed=true blocked=false stopped=false notLaunched=false enabled=0
    grantedPermissions:
      android.permission.INSTALL_PACKAGES
      android.permission.DELETE_PACKAGES
  Package [ds.cpuoverlay] (64f98b70):
    userId=10048 gids=[3003, 1028, 1015, 1023]
    pkg=Package{64fd32a8 ds.cpuoverlay}
    codePath=/data/app/ds.cpuoverlay-1.apk
    resourcePath=/data/app/ds.cpuoverlay-1.apk
    nativeLibraryPath=/data/app-lib/ds.cpuoverlay-1
    versionCode=83 targetSdk=22
    versionName=5.7.0
    applicationInfo=ApplicationInfo{6514bcb0 ds.cpuoverlay}
    flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]
    dataDir=/data/data/ds.cpuoverlay
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    timeStamp=2017-06-21 10:19:53
    firstInstallTime=2017-02-05 17:28:29
    lastUpdateTime=2017-06-21 10:19:55
    signatures=PackageSignatures{64f98c38 [64f999e8]}
    permissionsFixed=true haveGids=true installStatus=1
    pkgFlags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]
    User 0:  installed=true blocked=false stopped=false notLaunched=false enabled=0
    ....

1 个答案:

答案 0 :(得分:1)

你需要在长篇短语上进行两场比赛,但可以简化(并缩短)一下

dumpsys package packages | perl -nE'
    /^\s*Package \[(.*)\]/ && print "$1 -- "; /^\s*versionName=(.*)/ && say $1
' | sort

带输出

ds.cpuoverlay -- 5.7.0
org.fdroid.fdroid.privileged -- 0.1

我在您输入perl -nE '...' input.txt | sort

的文本文件中测试了您发布的输入