9

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

2 Answers 2

11

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.