Linux shell 脚本,解析每一行

Linux shell script, parsing each line

我的 shell 脚本(我正在使用 SH)遇到问题:

我有一个包含邮件地址的多行文件,例如:

abcd

plm

name_aA.2isurnamec@Text.com  -> this is a line that checks the correct condition

random efgh

aaaaaa

naaame_aB.3isurnamec@Text.ro   ->same (this is not part of the file)

我已经使用 grep 像这样过滤正确的邮件地址:

grep -E '^[a-z][a-zA-Z_]*.[0-9][a-zA-Z0-9]+@[A-Z][A-Z0-9]{, 12}.(ro|com|eu)$' file.txt

我必须编写一个 shell 来检查文件并打印以下内容(对于上面的示例,它应该是这样的):

"Incorrect:" abcd

"Incorrect:" plm

"Correct:" name_aA.2isurnamec@Text.com

"Incorrect:" random efgh

"Incorrect:" aaaaaa

"Correct:" naaame_aB.3isurnamec@Text.ro

我想使用 grep 或 sed 解决这个问题,而我不想使用列表或其他东西。

我试过用这样的东西

grep condition abc.txt | while read -r line ; do
    echo "Processing $line"
    # your code goes here
done

但它只打印正确的行,我知道我也可以在 grep 上使用 -v 打印不符合 grep 条件的行,但我想按照它们在文本中出现的顺序打印这些行文件。

我在尝试解析文件的每一行时遇到问题,或者我可能不需要解析第 1 行

by 1,我真的不知道怎么解决

如果你能帮助我,我将不胜感激。

谢谢

#!/bin/bash
pattern='^[a-z][a-zA-Z_]*\.[0-9][a-zA-Z0-9]+@[A-Z][A-Za-z0-9]{,12}\.(ro|com|eu)$'

while read line; do
    if [ "$line" ]; then
        if echo "$line" | grep -E -q $pattern; then
            echo "\"Correct:\" $line"
        else
            echo "\"Incorrect:\" $line"
        fi
    fi
done

像这样调用,假设调用 bash 脚本 filter 和文本文件 text.txt./filter < text.txt.

请注意,正则表达式中的句点已转义,并且域名可以包含小写字母(尽管我认为您的正则表达式过于严格)。其他字符未转义,因为字符串在单引号中。

while将标准输入逐行读入$line;第一个 if 跳过空行;第二个检查 $line$pattern-q 抑制 grep 输出)。