Reputation: 33
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. Since the last field (5th) has a space, I'm getting an error.
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. Using the cut
command is taking too long as I need to do this for about 130k lines.
Upvotes: 2
Views: 1198
Reputation: 361615
Use IFS=delim read -a array
to read the fields into an array, splitting on the character(s) in $IFS
.
while IFS='|' read -r -a fields; do
printf '[%s]\n' "${fields[@]}"
echo "${fields[0]}"
echo "${fields[1]}"
...
done < file.txt
Upvotes: 3