I need to write a Bash script to process a data file that is in this format:
1 A B C D E
2 F G "H H" I J
As you can see, the data is delimited by a space, but there are also some fields that contain spaces and are surrounded by double-quotes. An example of that is "H H".
I wrote this test script to display the 4th parameter:
#!/bin/bash
while read line
do
echo "line=$line"
param4=$(echo $line | cut -d" " -f4)
echo "param4=$param4"
done
Here's what it displays:
line=1 A B C D E
param4=C
line=2 F G "H H" I J
param4="H
For the second line of data, I wanted the fourth parameter to be "H H" (without the quotes) instead of one double quote and one H. It is using the other H and the trailing double quote as parameter 5. That is not what I wanted.
How can I process this data?