Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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

like image 547
sdinesh94 Avatar asked Mar 06 '17 20:03

sdinesh94


2 Answers

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"
like image 60
chepner Avatar answered Nov 11 '22 07:11

chepner


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"
like image 1
anubhava Avatar answered Nov 11 '22 06:11

anubhava