sdinesh94
sdinesh94

Reputation: 1178

How to parse a string with multiple characters to split on-Bash Scripting

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

Upvotes: 9

Views: 9578

Answers (2)

anubhava
anubhava

Reputation: 784998

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"

Upvotes: 1

chepner
chepner

Reputation: 530960

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"

Upvotes: 11

Related Questions