本文介绍了如何解析具有多个字符的字符串以拆分On-Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其内容如下:

I have a text file with the following contents:

我有一个脚本,可以按如下方式解析每行

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

解析如下:

DESTINATION => folder2/folder2_1/file1folder2.txt"

DESTINATION="> folder2/folder2_1/file1folder2.txt"

但是我想要的

DESTINATION ="folder2/folder2_1/file1folder2.txt"

DESTINATION="folder2/folder2_1/file1folder2.txt"

任何帮助.谢谢

推荐答案

IFS不是单个的多字符定界符;相反,它是一个单字符定界符的集合.您可以使用正则表达式将由任意定界符分隔的两个字段分开.

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"

这篇关于如何解析具有多个字符的字符串以拆分On-Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:16