【问题标题】:Shell stderr redirection to another file using a text file as a argument使用文本文件作为参数的 Shell stderr 重定向到另一个文件
【发布时间】:2016-07-01 19:21:19
【问题描述】:

我编写了一个 bash 脚本,该脚本将读取参数中已提供的文本文件,并处理文本并将错误重定向到错误文件,并将其他输出重定向到 list.txt 文件。

#!/bin/bash
filename="$1"
while read line; do
        a=$(echo $line | awk "{print NF}")
                if [ "$a" = "3" ]
                then
                        first=$(echo $line | awk -F' ' '{print $1}')
                        last=$(echo $line | awk -F' ' '{print $2}')
                        email=$(echo $line | awk -F' ' '{print $3}')
                        if [[ $first =~ ^[a-zA-Z]+$ && $last =~ ^[a-zA-Z]+$ ]]
                        then
                                if [[ $email =~ '<([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}>' ]]
                                then
                                        echo "$first $last $email" | cat >>list.txt
                                elif [[ $email =~ '([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}' ]]
                                then
                                        echo "$first $last <$email>" | cat >>list.txt
                                else
                                        echo "$first $last $email" | cat >&2
                                fi
                        else
                                echo "$first $last $email" | cat >&2
                        fi
                else
                        echo "$line" | cat >&2
                fi
        done < $filename

我将此代码作为 $./script.sh argumentfile.txt 2>error.txt

运行

我的参数文件有以下信息

Joe cable cable@ablecorp.com
Bob Brown <bob_baker@bakerandsons.com>
Jim Hass  hass@bigcorp.com
mike_lupo@mou.east.com
Edison jones jones@inl.net.gov
pirate.coe.su.com pirate people

文件的理想形式应为(故意格式错误)

lastname firstname <email> 

在错误文件中我得到的是

 Joe cable cable@ablecorp.com
Bob Brown <bob_baker@bakerandsons.com>
Jim Hass  hass@bigcorp.com
mike_lupo@mou.east.com
Edison jones jones@inl.net.gov
pirate.coe.su.com pirate people

【问题讨论】:

  • echo ... | cat 毫无意义。您不需要管道或cat
  • set -x 添加到您的脚本中,以便您查看实际运行的内容并尝试找出不匹配的模式,然后您可能会找出原因。
  • 您的正则表达式似乎有问题,使用命令检查它 - [[ $email =~ $reg ]] && echo "yes!"

标签: regex bash shell unix scripting


【解决方案1】:

你完全可以用awk来做到这一点:

#!/bin/bash

gawk '{

name_re = "^[[:alpha:]]+$"
mail_re = "<?[[:alnum:]_.%+-]+@[[:alnum:]_.-]+\\.[[:alpha:]]{2,6}>?"

# check for 3 fields with suitable regexp matching for all
if (NF == 3 && $1 ~ name_re && $2 ~ name_re && $3 ~ mail_re) {
    # put brackets around the address if needed
    email = $3 ~ /^<.*?>$/ ? $3 : "<" $3 ">"
    # output to the good list
    print $1 " " $2 " " email > "list.txt"
    # move to the next one
    next
}

# output to the bad list
print > "error.txt"

}' "$1"

使用 BSD 和 Gnu 版本的 awk 测试。

【讨论】:

  • 谢谢您,您的代码可以正常工作,我实际上做了一些更改,它也对我有用if [[ $email =~ [a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}) ]]
  • 除非你的 bash 脚本一团糟,而且你已经调用了 awk 4 次。您会发现脚本中出现的命令、管道和子 shell 越少,它的运行速度就越快。
猜你喜欢
  • 1970-01-01
  • 2016-03-29
  • 2011-07-03
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
相关资源
最近更新 更多