简体   繁体   English

需要在Linux中将制表符分隔的文本拆分为多个变量

[英]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.

相关问题 将一列分隔文本分成多列 - split a column of delimited text in multiple columns 从Linux中的文本文件中提取列(空格或制表符分隔) - Extracting columns (space or tab delimited) from text file in Linux 如何在制表符分隔的文本linux文件中的特定列中添加常量 - How to add constant to specific columns in tab delimited text linux file 使用从制表符分隔的文本文件(Linux)读取的参数运行的批处理脚本 - batch script that runs with parameters read from a tab delimited text file (Linux) 如何在Linux中根据记录数分割定界文本文件,该文件在数据字段中具有记录结尾分隔符 - How to Split a Delimited Text file in Linux, based on no of records, which has end-of-record separator in data fields Linux-将制表符分隔为管道分隔并删除前导和尾随空间 - Linux - convert tab delimited to pipe delimited AND remove leading & trailing space 替换选项卡分隔文件linux中的封闭字符串中的选项卡 - replace tab in an enclosed string in a tab delimited file linux 文本空间和制表符分隔的表格以分号分隔 - Text space and tab delimited table to semicolon separared 使用 linux 查找和替换制表符分隔文件中列中的 a 值 - Using linux to find and replace the a value in a column in a tab delimited file 使用 bash (linux) 选择制表符分隔文件的特定行 - Selecting specific rows of a tab-delimited file using bash (linux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM