Linux |分隔字段中的字符串值在awk中向右拆分

Linux |分隔字段中的字符串值在awk中向右拆分,linux,bash,shell,unix,awk,Linux,Bash,Shell,Unix,Awk,从给定文件中读取第2列时 在使用awk从给定文件中读取多列时,我遇到了一些问题。 阅读第2栏时,内容向右移动 /home/Binay/bin] 82#head -2 /data/xyz/serial/fep_xyz/temp/./xyz_reject_file_details_20180926194730.dat 309_body_mass_index_at_refresh.ABORT.2018-05-13.dat_SKIP_AT|Failed MBR_KEY Lookup|94 309_dis

从给定文件中读取第2列时 在使用awk从给定文件中读取多列时,我遇到了一些问题。 阅读第2栏时,内容向右移动

/home/Binay/bin] 82#head -2 /data/xyz/serial/fep_xyz/temp/./xyz_reject_file_details_20180926194730.dat
309_body_mass_index_at_refresh.ABORT.2018-05-13.dat_SKIP_AT|Failed MBR_KEY Lookup|94
309_disease_management_member_activity_at_refresh.ABORT.2018-05-13.dat_SKIP_AT|Failed MBR_KEY Lookup|11575
/home/Binay/bin] 82#
预期产量

/home/Binay/bin] 82#  cat /data/xyz/serial/fep_xyz/temp/./xyz_reject_file_details_20180926194730.dat | awk -F'|' ' {print $1,$2,$3} ' | while read abort_file abort_reason record_count                        <
> do
> echo ${abort_reason}
> done
Failed MBR_KEY Lookup
Failed MBR_KEY Lookup
/home/Binay/bin] 83#
/home/Binay/bin]82#cat/data/xyz/serial/fep_xyz/temp//xyz_reject_file_details_20180926194730.dat | awk-F |{print$1,$2,$3}|同时读取中止文件中止原因记录|<
>做
>echo${abort\u reason}
>完成
MBR_密钥查找失败
MBR_密钥查找失败
/home/Binay/bin]83#
但是我现在得到的输出是

/home/Binay/bin] 82#  cat /data/xyz/serial/fep_xyz/temp/./xyz_reject_file_details_20180926194730.dat | awk -F'|' ' {print $1,$2,$3} ' | while read abort_file abort_reason record_count                        <
> do
> echo ${abort_reason}
> done
Failed
Failed
/home/Binay/bin] 83#
/home/Binay/bin]82#cat/data/xyz/serial/fep_xyz/temp//xyz_reject_file_details_20180926194730.dat | awk-F |{print$1,$2,$3}|同时读取中止文件中止原因记录|<
>做
>echo${abort\u reason}
>完成
失败
失败
/home/Binay/bin]83#
当IFS='|'读取-r中止文件中止原因记录计数时;不回显${abort\u reason};完成<文件名

为什么不在while中直接使用输入字段分隔符?

第二个字段包含空格。初始awk脚本的作用基本上是用空格替换字段分隔符,因此
$abort\u reason
成为行中第二个空格分隔的单词。注意:

$ cat y.dat
a_b_c|two words|123
one_two|more words|234

$ awk -F'|' '{print $1,$2,$3}' y.dat
a_b_c two words 123
one_two more words 234

$ awk -F'|' '{print $1,$2,$3}' y.dat | while read a b c; do echo "$b"; done
two
more
与仅使用bash和填充数组以便于管理相比:

$ while IFS='|' read -a a; do declare -p a; done < y.dat
declare -a a=([0]="a_b_c" [1]="two words" [2]="123")
declare -a a=([0]="one_two" [1]="more words" [2]="234")
$ while IFS='|' read -a a; do declare -p a; done < y.dat
declare -a a=([0]="a_b_c" [1]="two words" [2]="123")
declare -a a=([0]="one_two" [1]="more words" [2]="234")
$ awk -F'|' '{gsub(/ /,"_"); print $1,$2,$3}' y.dat
a_b_c two_words 123
one_two more_words 234

$ awk -F'|' '{gsub(/ /,"_"); print $1,$2,$3}' y.dat | while read red green blue; do echo "$green"; done
two_words
more_words