sed正则表达式匹配['','WR'或'RN'] + 2-4位数

时间:2011-01-14 00:35:24

标签: regex unix text

我正在尝试在Unix上进行一些条件文本处理,并努力学习语法。我想要实现

Find the first 2, 3 or 4 digits in the string
if 2 characters before the found digits are 'WR' (could also be lower case)
    Variable = the string we've found (e.g. WR1234)
    Type = "work request"
else
    if 2 characters before the found digits are 'RN' (could also be lower case)
      Variable = the string we've found (e.g. RN1234)
      Type = "release note"
    else
      Variable = "WR" + the string we've found (Prepend 'WR' to the digits)
      Type = "Work request"
    fi
fi

我在Red Hat Enterprise Linux Server 5.5版(Tikanga)的Bash shell中这样做

提前致谢, 卡尔

3 个答案:

答案 0 :(得分:1)

我不确定你是如何阅读你的字符串的,但这个例子可以帮助你实现目标。我循环了4个示例字符串WR1234 RN456 7890 PQ2342。如果字符串与您的预期格式(我的示例中为PQ2342)不匹配,您没有说明该怎么做,所以我的代码只是忽略它。

#!/bin/bash

for string in "WR1234 - Work Request Name.doc" "RN5678 - Release Note.doc"; do
  [[ $string =~ ^([^0-9]*)([0-9]*).*$ ]]
  case ${BASH_REMATCH[1]} in
    "WR")
          var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          type="work request"
          echo -e "$var\t-- $type"
          ;;
    "RN")
          var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          type="release note"
          echo -e "$var\t-- $type"
          ;;
    "")
          var="WR${BASH_REMATCH[2]}"
          type="work request"
          echo -e "$var\t-- $type"
          ;;
  esac
done

输出

$ ./rematch.sh
WR1234  -- work request
RN5678  -- release note

答案 1 :(得分:1)

我喜欢使用perl -pe代替sed,因为PERL具有如此富有表现力的正则表达式。为了教学,以下内容有点冗长。

example.txt

WR1234 - Work Request name.doc
RN456
rn456
WR7890 - Something else.doc
wr789
2456

script.sh

#! /bin/bash

# search for 'WR' or 'RN' followed by 2-4 digits and anything else, but capture 
# just the part we care about
records="`perl -pe 's/^((WR|RN)([\d]{2,4})).*/\1/i' example.txt`"

# now that you've filtered out the records, you can do something like replace 
# WR's with 'work request'
work_requests="`echo \"$records\" | perl -pe 's/wr/work request /ig' | perl -pe 's/rn/release note /ig'`"

# or add 'WR' to lines w/o a listing
work_requests="`echo \"$work_requests\" | perl -pe 's/^(\d)/work request \1/'`"

# or make all of them uppercase
records_upper=`echo $records | tr '[:lower:]' '[:upper:]'`

# or count WR's
wr_count=`echo "$records" | grep -i wr | wc -l`
echo count $wr_count

echo "$work_requests"

答案 2 :(得分:1)

#!/bin/bash
string="RN12344 - Work Request Name.doc"
echo "$string" | gawk --re-interval '
{
    if(match ($0,/(..)[0-9]{4}\>/,a ) ){
        if (a[1]=="WR"){
            type="Work release"
        }else if  ( a[1] == "RN" ){
            type = "Release Notes"
        }
        print type
    }
}'