如何使用Shell脚本获取匹配的字符串文件指针位置

我必须阅读具有html内容的txt文件。匹配字符串patten,如果匹配,则获取文本文件中下一行的所有内容。

混淆了要使用的shell命令。使用EXEC,CAT ..找到了一些示例代码,但无法获得我的预期输出。

这是webdata.txt中的内容

<li id="rowForcustomfield_10003" class="item">
    <div class="wrap">
        <strong title="Name" class="name">Name:</strong>
        <div id="customfield_10003-val" class="value type-cascadingselect" data-fieldtype=
        "cascadingselect" data-fieldtypecompletekey="com.atlassian.jira.plugin.system.
        customfieldtypes:cascadingselect">  JAMES   - WELLIS </div>
    </div>
</li>

如果字符串“名称”匹配,那么我需要保存“ JAMES-WELLIS”

文本文件中的几个名称也显示如下,带有多个空格和换行符。

<div class="wrap">
    <strong title="Architecture/Derivate" class="name">Name:</strong>
    <div id="customfield_10003-val" class="value type-cascadingselect" data-fieldtype="cascadingselect" data-fieldtypecompletekey="com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect">
                                GREG
     R GEORGE

                    </div>
</div>

这是我找到的示例代码

  do
    if [[ $(grep ">Name:"  $line) ]];
    then
     echo "$line + 1"    # get next line data from webdata.txt
    fi
  done < webdata.txt
fdsafdsaga 回答:如何使用Shell脚本获取匹配的字符串文件指针位置

您可以尝试如下所示的简单操作,它将所有名称提取到名为output.txt的文件中

 while read line 
 do  
    [[ "$line" == *"Name"* ]] && continue 
    echo "$line"| cut -d'>' -f2 | cut -d'<' -f1 >> output.txt 
 done < <(grep -A1 'Name' webData.txt)

编辑

对于不允许进程替换的shell:

grep -A1 'Name' webData.txt | while read line
 do
    [[ "$line" == *"Name"* ]] && continue
    echo "$line"| cut -d'>' -f2 | cut -d'<' -f1 >> output1.txt
 done
本文链接:https://www.f2er.com/3139077.html

大家都在问