linux - 使用 SQL 获取的拆分管道分隔字符串

标签 linux string shell

我的 shell 脚本执行 SQL 以获取以下格式的数据:

JOB_ID_001|[PROD] This is a mail subject one ${application_date}|a@example.com,b@example.com
JOB_ID_002|[PROD] This is a mail subject two ${application_date}|c@example.com,b@example.com

我想拆分这个管道分隔的字符串,但输出看起来很奇怪,如下所示:

JOB_ID_001[0]
JOB_ID_001[1]
JOB_ID_001[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
one[0]
one[1]
one[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
example.com,b@example.com[0]
example.com,b@example.com[1]
example.com,b@example.com[2]
JOB_ID_002[0]
JOB_ID_002[1]
JOB_ID_002[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
two[0]
two[1]
two[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
ple.com,b@example.com[0]
ple.com,b@example.com[1]
ple.com,b@example.com[2]

我想要的输出是:

JOB_ID_001
[PROD] This is a mail subject one ${application_date}
a@example.com,b@example.com
JOB_ID_002
[PROD] This is a mail subject two ${application_date}
c@example.com,b@example.com

这样我就可以继续处理这些字符串了。

我的shell脚本如下:

email_configs=(`sqlplus -silent $DB_CONN <<-EOF
    whenever sqlerror exit 1 oserror exit oscode
    set heading off feedback off echo off verify off pagesize 0
    $sql_subject_of_mail;
    exit;
    EOF`)

for i in "${!email_configs[@]}"
do
    email_config=${email_configs[i]}

    IFS='|' read -r -a email_config_array <<< "$email_config"

    job_id=$email_config_array[0]
    subject_of_mail=$email_config_array[1]
    to_mail_id=$email_config_array[2]

    echo $job_id
    echo $subject_of_mail
    echo $to_mail_id

done

我检查了来自 this 的一些替代解决方案页面,但在输出中缺少 ${application_date} 部分或存在其他问题。

谁能知道我的错误?

最佳答案

$email_configs 数组设置不正确。它使用空格作为数组分隔符,而不是换行符。

与其设置数组,不如循环读取 sqlplus 的输出。

while IFS='|' read -r job_id subject_of_mail to_mail_id
do
    echo "$job_id"
    echo "$subject_of_mail"
    echo "$to_mail_id"
done < <(sqlplus -silent $DB_CONN <<-EOF
    whenever sqlerror exit 1 oserror exit oscode
    set heading off feedback off echo off verify off pagesize 0
    $sql_subject_of_mail;
    exit;
    EOF 
)

关于linux - 使用 SQL 获取的拆分管道分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55931638/

相关文章:

linux - 如何删除 600 GB 的小文件?

linux - 是否可以使用 madwifi 驱动程序干扰 wifi 中 MAC 协议(protocol)的 header ?

linux - Gradle 不显示任何控制台输出

shell - 在后台 shell 脚本中使用 netcat/cat(如何避免停止(tty 输入)?)

linux - 提取音频,再次操作和合并

python - 如何在 ubuntu 中将 python 2.6 更新为 python 2.7

c - 如何将格式化字符串附加到数组中?

php - SQL求职功能问题

Python 将字符串和 boolean 值转换为列表

regex - 如何通过 ftp 连接获取字母数字大于 Linux 命令行 (bash) 中指定的文本的文件列表?