在ksh脚本中使用正则表达式

问题描述 投票:0回答:3

我有一个文件(file.txt),其中包含一些文本,如:

  • 000000000+000+0+00
  • 000000001+000+0+00
  • 000000002+000+0+00

我试图检查每一行,以确保它遵循格式:字符* 9,“+”,字符* 3,“+”等

到目前为止我有:

#!/bin/ksh
file=file.txt
line_number=1
for line in $(cat $file)
do
    if [[ "$line" != "[[.]]{9}+[[.]]{3}+[[.]]{1}+[[.]]{2} ]" ]]
    then
        echo "Invalid number ($line) check line $line_number"
        exit 1
    fi
    let "line_number++"
done

但无论我在程序终止的行中放置什么,这都无法正确评估。

regex unix ksh
3个回答
0
投票

我正在把正则表达式直接置于条件中有趣的结果:

$ line='000000000+000+0+00'
$ [[ $line =~ ^.{9}\+.{3}\+.\+..$ ]] && echo ok
ksh: syntax error: `~(E)^.{9}\+.{3}\+.\+..$ ]] && echo ok
' unexpected

但是,如果我将正则表达式保存在变量中:

$ re="^.{9}\+.{3}\+.\+..$"
$ [[ $line =~ $re ]] && echo ok
ok

所以你可以做到

#!/bin/ksh
file=file.txt
line_number=1
re="^.{9}\+.{3}\+.\+..$"
while IFS= read -r line; do
    if [[ ! $line =~ $re ]]; then
        echo "Invalid number ($line) check line $line_number"
        exit 1
    fi
    let "line_number++"
done < "$file"

您还可以使用普通的glob模式:

if [[ $line != ?????????+???+?+?? ]]; then echo error; fi

ksh glob模式有一些类似regex的语法。如果那里有可选空格,您可以使用?(sub-pattern)语法处理它

pattern="?????????+???+?( )?+??"

line1="000000000+000+0+00"
line2="000000000+000+ 0+00"

[[ $line1 == $pattern ]] && echo match || echo no match  # => match
[[ $line2 == $pattern ]] && echo match || echo no match  # => match

阅读ksh手册页的“文件名生成”部分。


1
投票

如果需要不匹配的行号,可以使用grep -vn。小心写一个正确的正则表达式,你会有

grep -Evn "^.{9}[+].{3}[+].[+].{2}$" file.txt 

这不在您想要的布局中,因此使用sed更改布局:

grep -Evn "^.{9}[+].{3}[+].[+].{2}$" file.txt |
   sed -r 's/([^:]*):(.*)/Invalid number (\2) check line number \1./'

编辑: 我把.{1}变成了.sed也位居榜首。当你需要spme解释时,你可以从echo "Linenr:Invalid line"开始


0
投票

你的正则表达式看起来bad - 使用像https://regex101.com/这样的网站是非常有帮助的。根据你的描述,我怀疑它看起来应该更像是其中之一;

  • ^.{9}\+.{3}\+.{1}\+.{2}$
  • ^[^\+]{9}\+[^\+]{3}\+[^\+]{1}\+[^\+]{2}$
  • ^[0-9]{9}\+[0-9]{3}\+[0-9]{1}\+[0-9]{2}$

来自ksh manpage[[部分 - 你可能想要使用=~

string =~ ere
    True if string matches the pattern ~(E)ere where ere is an extended regular expression.

注意:据我所知,ksh正则表达式不遵循正常的语法


使用grep可能会更好运:

# X="000000000+000+0+00"
# grep -qE "^[^\+]{9}\+[^\+]{3}\+[^\+]{1}\+[^\+]{2}$" <<<"${X}" && echo true
true

要么:

if grep -qE "^[^\+]{9}\+[^\+]{3}\+[^\+]{1}\+[^\+]{2}$" <<<"${line}"
then
    exit 1
fi

您可能还希望使用类似下面的结构来处理文件:

while read line; do
    echo "${line}";
done < "${file}"
© www.soinside.com 2019 - 2024. All rights reserved.