Bash 在文本文件中循环匹配的字符串

Bash 在文本文件中循环匹配的字符串,bash,loops,Bash,Loops,如何通过字符串“BUILD#”循环,并使用bash脚本仅打印包含以下字符串“http://”的行? 或者,我们如何让字符串之间的所有内容“构建” input.txt BUILD #1 http://a b http://c BUILD #2 http://d http://c http://g BUILD #3 g http://h http://f e http://t 期望输出 编者按:正如所接受的答案所证明的那样,问题不是关于生成过滤输出,而是关于以一种知道封闭构建上下文的方式选择

如何通过字符串“BUILD#”循环,并使用bash脚本仅打印包含以下字符串“http://”的行? 或者,我们如何让字符串之间的所有内容“构建” input.txt

BUILD #1
http://a
b
http://c

BUILD #2
http://d
http://c
http://g


BUILD #3
g
http://h
http://f
e
http://t
期望输出

编者按:正如所接受的答案所证明的那样,问题不是关于生成过滤输出,而是关于以一种知道封闭构建上下文的方式选择性地处理行, 如前面相应的
BUILD 35;
行所示


input.txt具有合并请求列表

BUILD #1
take the link (integrate)
take the link (integrate)
kick of a build (build command)
BUILD #2 
take the link (integrate)
take the link (integrate)
take the link (integrate)
kick of a build (build command)
and so on 

您可以这样循环它:

while read -r; do
   # if current line starts with BUILD then set a variable build with build #
   [[ $REPLY == BUILD* ]] && build="${REPLY#BUILD }" && continue

   # if current line starts with http:// then execute a command using build # & link
   [[ $REPLY == http://* ]] && printf "Build: %s -> Link: %s\n" "$build" "$REPLY"
done < input.txt

Build: #1 -> Link: http://a
Build: #1 -> Link: http://c
Build: #2 -> Link: http://d
Build: #2 -> Link: http://c
Build: #2 -> Link: http://g
Build: #3 -> Link: http://h
Build: #3 -> Link: http://f
Build: #3 -> Link: http://t
读取时-r;做
#如果当前行以BUILD开头,则使用BUILD设置变量BUILD#
[[$REPLY==BUILD*]]和&BUILD=“${REPLY#BUILD}”和&continue
#如果当前行以http://开头,则使用build#&link执行命令
[[$REPLY==http://*]&&printf“生成:%s->Link:%s\n”“$Build”“$REPLY”
完成链接:http://a
构建:#1->链接:http://c
构建:#2->链接:http://d
构建:#2->链接:http://c
构建:#2->链接:http://g
构建:#3->链接:http://h
构建:#3->链接:http://f
构建:#3->链接:http://t

您可以将
printf
更改为所需的任何其他命令。

您可以这样循环它:

while read -r; do
   # if current line starts with BUILD then set a variable build with build #
   [[ $REPLY == BUILD* ]] && build="${REPLY#BUILD }" && continue

   # if current line starts with http:// then execute a command using build # & link
   [[ $REPLY == http://* ]] && printf "Build: %s -> Link: %s\n" "$build" "$REPLY"
done < input.txt

Build: #1 -> Link: http://a
Build: #1 -> Link: http://c
Build: #2 -> Link: http://d
Build: #2 -> Link: http://c
Build: #2 -> Link: http://g
Build: #3 -> Link: http://h
Build: #3 -> Link: http://f
Build: #3 -> Link: http://t
读取时-r;做
#如果当前行以BUILD开头,则使用BUILD设置变量BUILD#
[[$REPLY==BUILD*]]和&BUILD=“${REPLY#BUILD}”和&continue
#如果当前行以http://开头,则使用build#&link执行命令
[[$REPLY==http://*]&&printf“生成:%s->Link:%s\n”“$Build”“$REPLY”
完成链接:http://a
构建:#1->链接:http://c
构建:#2->链接:http://d
构建:#2->链接:http://c
构建:#2->链接:http://g
构建:#3->链接:http://h
构建:#3->链接:http://f
构建:#3->链接:http://t
您可以将
printf
更改为所需的任何其他命令。

显示了如何按顺序迭代这些行,并在输入每个特定于生成的行块时记录当前生成编号

这里有一个解决方案,它以行块的形式逐个构建处理行,因此,如果需要,您可以应用构建级别的操作

所有的块都是按顺序处理的,但是将解决方案调整为只针对特定的构建数量并不困难

#!/bin/bash

buildNum= urls=() finalIteration=0
while IFS= read -r line || { finalIteration=1; true; }; do
   # A new build block is starting or EOF was reached.
  if [[ $line =~ ^'BUILD #'([0-9]+) || $finalIteration -eq 1 ]]; then
    if [[ $buildNum ]]; then # Process the previous block.
      echo "Processing build #$buildNum..."
      # Process all URLs.
      i=0
      for url in "${urls[@]}"; do
        echo "Url #$((++i)): $url"
      done
      # Add further per-build processing here...
    fi    
    (( finalIteration )) && break # Exit the loop, if EOF reached.
    # Save this block's build number.
    buildNum=${BASH_REMATCH[1]}
    urls=() # Reset the array of URLs.
  # Collect the lines of interest in array ${url[@]}, for later processing.
  elif [[ $line =~ ^http:// ]]; then
    urls+=( "$line" )
  fi
done < input.txt
#/bin/bash
buildNum=URL=()finalIteration=0
而IFS=read-r行| |{finavertision=1;true;};做
#新生成块正在启动或已达到EOF。
如果[[$line=~^'BUILD#'([0-9]+)| |$finalertification-eq 1]];然后
如果[$buildNum]];然后#处理上一个块。
echo“正在处理构建#$buildNum…”
#处理所有URL。
i=0
对于“${url[@]}”中的url;做
回显“Url#$(++i)):$Url”
完成
#在此处添加进一步的每次生成处理。。。
fi
((finalIteration))&&break#如果达到EOF,则退出循环。
#保存此块的内部版本号。
buildNum=${BASH_重新匹配[1]}
URL=()#重置URL数组。
#收集数组${url[@]}中感兴趣的行,以便以后处理。
elif[[$line=~^http://];然后
URL+=(“$line”)
fi
完成
向您展示了如何按顺序迭代这些行,在输入每个特定于生成的行块时记录当前生成编号

这里有一个解决方案,它以行块的形式逐个构建处理行,因此,如果需要,您可以应用构建级别的操作

所有的块都是按顺序处理的,但是将解决方案调整为只针对特定的构建数量并不困难

#!/bin/bash

buildNum= urls=() finalIteration=0
while IFS= read -r line || { finalIteration=1; true; }; do
   # A new build block is starting or EOF was reached.
  if [[ $line =~ ^'BUILD #'([0-9]+) || $finalIteration -eq 1 ]]; then
    if [[ $buildNum ]]; then # Process the previous block.
      echo "Processing build #$buildNum..."
      # Process all URLs.
      i=0
      for url in "${urls[@]}"; do
        echo "Url #$((++i)): $url"
      done
      # Add further per-build processing here...
    fi    
    (( finalIteration )) && break # Exit the loop, if EOF reached.
    # Save this block's build number.
    buildNum=${BASH_REMATCH[1]}
    urls=() # Reset the array of URLs.
  # Collect the lines of interest in array ${url[@]}, for later processing.
  elif [[ $line =~ ^http:// ]]; then
    urls+=( "$line" )
  fi
done < input.txt
#/bin/bash
buildNum=URL=()finalIteration=0
而IFS=read-r行| |{finavertision=1;true;};做
#新生成块正在启动或已达到EOF。
如果[[$line=~^'BUILD#'([0-9]+)| |$finalertification-eq 1]];然后
如果[$buildNum]];然后#处理上一个块。
echo“正在处理构建#$buildNum…”
#处理所有URL。
i=0
对于“${url[@]}”中的url;做
回显“Url#$(++i)):$Url”
完成
#在此处添加进一步的每次生成处理。。。
fi
((finalIteration))&&break#如果达到EOF,则退出循环。
#保存此块的内部版本号。
buildNum=${BASH_重新匹配[1]}
URL=()#重置URL数组。
#收集数组${url[@]}中感兴趣的行,以便以后处理。
elif[[$line=~^http://];然后
URL+=(“$line”)
fi
完成