【问题标题】:How to grep for the whole string after a word in the string is matched匹配字符串中的单词后如何对整个字符串进行grep
【发布时间】:2016-10-01 04:27:07
【问题描述】:

我有这个

-Xmx10240m -Xms10240m -verbose:gc -XX:+CMSParallelRemarkEnabled -XX:+UseParNewGC -XX:+ScavengeBeforeFullGC -Dsun.net.inetaddr.ttl=3600 -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX: +PrintTenuringDistribution -XX:SurvivorRatio=6 -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:PermSize=512m -XX:MaxPermSize=512m -Xloggc:/www/logs/jboss/ macys-navapp_master_prod_cellA_m01/gc-log.txt -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/www/logs/heapdump/navapp_master_prod_cellA__m01/navapp_master_prod_cellA_m01.hprof -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true - Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -XX:+UseCompressedOops -Dclient.encoding.override=ISO-8859-1 -XX:+DisableExplicitGC -Dorg.apache.jasper。 Constants.USE_INSTANCE_MANAGER_FOR_TAGS=false -Dorg.apache.jasper.Constants.USE_INSTANCE_MANAGER -Dorg.apache.jasper.runtime.JspFactoryImpl.USE_POOL=false -Dorg.apache.j asper.runtime.BodyContentImpl.LIMIT_BUFFER=true -XX:NewSize=3072m -XX:MaxNewSize=3072m -agentpath:/www/a/apps/dynatrace/dt.so=name=server1_ProdCellA_master_m1,server=ct_collector:9998 -Dfile.encoding =ISO-8859-1 -Dsdp.configuration.home=/www/apps/properties -XX:+UseLargePages -Dzookeeper.sasl.client=false

我希望能够在匹配 "-agentpath" "-agentpath:/www/a/apps/dynatrace/dt.so=name=server1_ProdCellA_master_m1,server=ct_collector:9998" 后 grep 整个字符串

这是我正在使用的当前命令,但它不起作用“cat cached_java_opts | awk '/-agentpath/ {print $(NF)}'”

谢谢你

【问题讨论】:

    标签: unix awk grep


    【解决方案1】:

    awk解决方案:

    awk -F'-agentpath:' '{split($2,a," ") ;print FS a[1]}' infile
    -agentpath:/www/a/apps/dynatrace/dt.so=name=server1_ProdCellA_master_m1,server=ct_collector:9998
    

    grep :或多或少,与已经回答的相同。

    grep -oP '\-agentpath:.*?\s' infile
    -agentpath:/www/a/apps/dynatrace/dt.so=name=server1_ProdCellA_master_m1,server=ct_collector:9998
    

    【讨论】:

      【解决方案2】:

      像这样启动grep

      grep -o '\-agentpath[^ ]*' yourfile
      

      -o 选项仅打印匹配模式(而不是匹配行)。由于模式被配置为扩展到第一个空格,因此您将获得整个参数(这有效,因为它不是命令行的最后一个参数)。也许可以通过grep -oE '\-agentpath([^ ]*|.*$)' 改进

      【讨论】:

      • 感谢您的回复,但看起来像“-agenth”,它认为这些是更多的 grep 选项,因为“-”,即使我在它周围加上了“-”
      • 追加转义字符如 grep -o '\-agentpath[^ ]*' yourfile
      • 抱歉,无法在这里测试它,因为我工作时的 grep 没有 -o 选项(它是最近的)。已编辑。现在了解-agentname。 @muzido 感谢您的纠正!
      • 那份工作我也忘了转义了lolz,非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多