I have a text file with the following contents:
folder4/file2folder4.txt >> folder2/folder2_1/file1folder2.txt
I have a script that parses each lines as follows
while IFS=" >> " read -r src dest
do
if [ "$src" != "" ]; then
SOURCEFILES+=("$src")
fi
if [ "$dest" != "" ]; then
DESTINATIONFILES+=("$dest")
fi
done < $TransferDescriptor
It parses as follows:
SOURCEFILE="folder4/file2folder4.txt"
DESTINATION="> folder2/folder2_1/file1folder2.txt"
But what I want
SOURCEFILE="folder4/file2folder4.txt"
DESTINATION="folder2/folder2_1/file1folder2.txt"
Any help. Thanks
IFS
is not a single, multicharcter delimiter; instead, it is a collection of single-character delimiters. You can use a regular expression to break apart two fields separated by an arbitrary delimiter.
regex='(.*) >> (.*)'
while IFS= read -r line dest
do
[[ $line =~ $regex ]] || continue
src=${BASH_REMATCH[1]}
dest=${BASH_REMATCH[2]}
if [[ -n $src ]]; then
SOURCEFILES+=("$src")
fi
if [[ -n $dest ]]; then
DESTINATIONFILES+=("$dest")
fi
done < "$TransferDescriptor"
You cannot multiple character string in IFS
. It only supports single characters to be used as input field separator.
You can make use of BASH regular expressions here like this:
while IFS= read -r line; do
if [[ $line =~ $re ]] ; then
srcfile="${BASH_REMATCH[1]}"
dest="${BASH_REMATCH[2]}"
fi
done < file
Check our variable:
declare -p srcfile dest
declare -- srcfile="folder4/file2folder4.txt"
declare -- dest="folder2/folder2_1/file1folder2.txt"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With