How to create an array of double quoted strings in a shell script

I am trying to split a double quoted string into an array:

input.txt:

"ABC" "This is TEST 1" "12.3.0"    
"AC" "This is TEST 221" "123"    
"CX" "This is TEST 16" "123.2"    
"LM" "This is TEST 9000" "123.6.6.1"

      

What I hope to be the result for each line:

print $a[0] $a[1] $a[2]

ABC This is TEST 1 12.3.0

      

What's the best way to capture every line in a line? I am trying to do this via command line and / or shell script

Update: To reduce the complexity, I updated the "input.txt" file as follows: input.txt:

'ABC' 'This is TEST 1' '12.3.0'    
'AC' 'This is "TEST" 221' '123'    
'CX' 'This is TEST 16' '123.2'    
'LM' 'This is TEST 9000' '123.6.6.1'

      

All double quotes have been replaced with single quotes, others are those that have a meaning.

+3


source to share


1 answer


Assuming you are using bash:

IFS='"' a=("ABC" "This is TEST1" "12.3.0")

      

should almost work. Indexes will be disabled with empty inputs, but:



 while IFS='"' read -a a; do 
    echo ${a[1]} ${a[3]} ${a[5]}; done < input

      

will get you most of the journey. Keep in mind that this is quite fragile.

+1


source







All Articles