[英]need to split tab delimited text into multiple variables in linux
I need to split a line into multiple variables within single line of command. 我需要在一行命令中将一行拆分为多个变量。 I need to fetch each field between tabs into a separate variable.
我需要将制表符之间的每个字段都提取到一个单独的变量中。 For example:
例如:
000345AA6| |N|20150410|A & M FRNT 3/01/17|
I'm using awk to fetch each field and then eval
to export variable to shell from awk. 我正在使用awk获取每个字段,然后使用
eval
将变量从awk导出到shell。 Since the last field (5th) has a space, I'm getting an error. 由于最后一个字段(第5个)有空格,因此出现错误。
Code: 码:
z=`echo "000380105| |%|20150410|ABCAM PLC ADR |" | awk -F "|" '{print "SOURCE_FIELD2="$2;SOURCE_FIELD3=$3;print "SOURCE_FIELD4="$4;SOURCE_FIELD5=$5;}'`
eval "$z"
echo $SOURCE_FIELD4
Actual Output: 实际输出:
test.ksh[12]: PLC: not found
Expected: 预期:
ABCAM PLC ADR
I have 19 fields in the line and want to assign variables with good performance. 我在该行中有19个字段,并希望分配性能良好的变量。 Using the
cut
command is taking too long as I need to do this for about 130k lines. 使用
cut
命令花费的时间太长,因为我需要大约13万行。
Use IFS=delim read -a array
to read the fields into an array, splitting on the character(s) in $IFS
. 使用
IFS=delim read -a array
将字段读入数组,分割$IFS
的字符。
while IFS='|' read -r -a fields; do
printf '[%s]\n' "${fields[@]}"
echo "${fields[0]}"
echo "${fields[1]}"
...
done < file.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.