String Shell在字符串中的数字之前提取文本

String Shell在字符串中的数字之前提取文本,string,shell,text,digits,String,Shell,Text,Digits,我已经找到了几个在单个字符之前提取的例子和提取数字的例子,但是我还没有找到任何关于在数字之前提取字符的例子 我的问题: 我使用的一些字符串如下所示: NUC320 Syllabus Template - 8wk SLA School Template - UL CJ101 Syllabus Template - 8wk TECH201 Syllabus Template - 8wk Test Clone ID17 如果字符串不包含我想要的数据,我需要跳过它。所需的输出将是: NUC-320 C

我已经找到了几个在单个字符之前提取的例子和提取数字的例子,但是我还没有找到任何关于在数字之前提取字符的例子

我的问题: 我使用的一些字符串如下所示:

NUC320 Syllabus Template - 8wk
SLA School Template - UL
CJ101 Syllabus Template - 8wk
TECH201 Syllabus Template - 8wk
Test Clone ID17
如果字符串不包含我想要的数据,我需要跳过它。所需的输出将是:

NUC-320
CJ-101
TECH-201
NUC-320
CJ-101
TECH-201
SLA学校模板-UL
&
将跳过测试克隆ID17

我想象这个过程的效果是:

    #!/bin/sh

    # my files are named 1.txt through 9999.txt i both 
    # increments the loop and sets the filename to be searched

    i=1

    while [ $i -lt 10000 ]
    do
        x=$(head -n 31 $i.txt | tail -1 | cut -c 7-)
        if [ ! -z "$x" -a "$x" != " " ]; then
# I'd like to insert the hyperlink with the output on the
# same line (1.txt;cj101 Syllabus Template - 8wk;www.link.com/cj101)
            echo "$i.txt;$x" >> syllabus.txt
    #   else
    #       rm $i.txt
        fi
        i=`expr $i + 1`
        sleep .1
    done
  • 在“”之前提取文本
  • 条件-检查字符串中的数字
  • 提取数字前的文本并将其分配给变量
    x
  • 提取数字并分配给变量
    y
  • 连接
    $x“-“$y
    并分配给另一个变量
    z
  • 更多信息: 使用循环从数千个文本文档中的一行中提取字符串。它们将用于在循环过程中附加到超链接并重命名文件

    编辑:

        #!/bin/sh
    
        # my files are named 1.txt through 9999.txt i both 
        # increments the loop and sets the filename to be searched
    
        i=1
    
        while [ $i -lt 10000 ]
        do
            x=$(head -n 31 $i.txt | tail -1 | cut -c 7-)
            if [ ! -z "$x" -a "$x" != " " ]; then
    # I'd like to insert the hyperlink with the output on the
    # same line (1.txt;cj101 Syllabus Template - 8wk;www.link.com/cj101)
                echo "$i.txt;$x" >> syllabus.txt
        #   else
        #       rm $i.txt
            fi
            i=`expr $i + 1`
            sleep .1
        done
    

    符合POSIX的
    awk
    解决方案:

    awk '{ if (match($1, /[0-9]+$/)) print substr($1, 1, RSTART-1) "-" substr($1, RSTART) }' \
      file | 
        while IFS= read -r token; do
          # Process token here (append to hyperlink, ...)
          echo "[$token]"
        done
    
    • awk
      用于提取重新格式化的感兴趣的令牌,然后在shell
      while循环中处理这些令牌
    • match($1,/[0-9]+$/)
      将第一个空格分隔字段(
      $1
      )与扩展正则表达式
      [0-9]+$
      匹配,即仅当字段以一个或多个数字结尾时匹配
    • substr($1,1,RSTART-1)“-”substr($1,RSTART)
      通过特殊的
      RSTART
      变量将第一个数字之前的部分与一系列数字连接起来,该变量指示最新的
      match()
      调用匹配的基于1的字符位置

    用于打印以大写字母开头,后跟数字的行。它还在它们之间添加了一个
    -

    sed -n 's/^\([A-Z]\+\)\([0-9]\+\) .*/\1-\2/p' input 
    
    给出:


    做得很好,但是您使用的是GNU-Sed语法-
    \+
    不可移植;相反,使用
    \{1,\}
    使命令可移植:
    sed-n's/^\([A-Z]\{1,\}\)([0-9]\{1,\}\)./\1-\2/p'input
    。我正在努力实现这个命令,因为正在编辑的是一个变量,而不是文件中的字符串。我正在编辑我的问题以获得更好的图片。通过使用
    echo“$x”| sed-n的/^\([a-Z]\{1,\}\)\([0-9]\{1,\}\)./\1-\2/p'
    来修改变量。我正在努力实现这一点,因为正在编辑的是一个变量,而不是来自文件的字符串。我正在编辑我的问题,以便更好地理解。@Anderb7583:我的荣幸;很高兴听到你成功了。