【问题标题】:Find string in a file after a string pattern using shell script使用 shell 脚本在字符串模式后查找文件中的字符串
【发布时间】:2014-10-17 10:35:46
【问题描述】:

我的输出文件有 4 行

storefront/storefront.war/location/header-info.jsp:30:<input type="hidden" id="welcomeConfigValue" value="${welcomeConfig}"/>
storefront/storefront.war/location/header-info.jsp:31:<span id="selected-location" class="top-txt top-nav-fix">
storefront/storefront.war/location/header-info.jsp:33:<span id="headRestName"></span><span class="header-spacing"> | </span><span id="headRestPhone"></span><span class="header-spacing"> | </span>
storefront/storefront.war/location/header-info.jsp:35:<a href="#" class="capitalize link-wht" id="location-show"><fmt:message

我想在 UNIX shell 中获得id= 之后的输出字符串。

即输出应该是这样的:

welcomeConfigValue
selected-location
headRestName
headRestPhone
location-show

【问题讨论】:

    标签: regex shell


    【解决方案1】:

    你可以试试grep:

    grep -Po '\sid="\K[^"]*' file
    

    【讨论】:

      【解决方案2】:

      命令:

      sed -r 's/(^.*id=")([^"]+)(.*$)/\2/g' < file.txt
      

      输出:

      sdlcb@Goofy-Gen:~/AMD$ sed -r 's/(^.*id=")([^"]+)(.*$)/\2/g' < ff.txt
      welcomeConfigValue
      selected-location
      headRestPhone
      location-show
      

      在这里,我们使用 "(" & ")" 将模式分为 3 组。第一组包含从行首到 'id="' 的所有字符。第二组包含 "s" 之间的字符(即 'id="' 和对 '"' 之间)。第三组包含直到行尾的剩余字符。然后我们就避免第一种和第三种模式。

      【讨论】:

      • 未命中headRestName
      • tr " " "\n"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2013-11-13
      • 2011-10-11
      • 2012-11-02
      • 2019-11-23
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多