世界"," IFS分离像&QUOT的字符串;你好" QUOT,与此",&QUOT是,一个镗QUOT;,"行" [英] IFS separate a string like "Hello","World","this","is, a boring", "line"

查看:131
本文介绍了世界"," IFS分离像&QUOT的字符串;你好" QUOT,与此",&QUOT是,一个镗QUOT;,"行"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析.csv文件,我有一些问题IFS。
该文件包含这样的行:

 你好,世界,这个,是一个无聊,线

中的列用逗号分隔的,所以我试图用爆炸这个code线:

  IFS =读-r -a tempArr<<< $线

不过,我得到这样的输出:

 你好
世界
这个

一个无聊
线

我明白为什么,所以我尝试了一些其他的命令,但我没有得到我预期的产出。

  IFS = \\,\\
IFS = \\
IFS =',\\'
IFS = \\

每一次的第三个元素是两部分分开。
如何使用IFS来单独像这样的字符串到5份?

 你好
世界
这个
是,一个无聊
线


解决方案

bashlib 提供了 csvline 功能。假设你已经在你的PATH中的某个地方安装了它:

 行=你好,世界,这个,是一个无聊,线来源bashlib
csvline<<<$线
printf的'%s的\\ n'$ {CSVLINE [@]}

...从上面被输出:

 你好
世界
这个
是一个无聊
线


要引用执行(这是版权 lhunath ,正在从相关混帐回购协议这一特定版​​本):

 #_______________________________________________________________________
#| __ csvline ____________________________________________________________ |

#csvline [-D分隔符] [-D行分隔符]

#解析从标准输入CSV记录,存储阵列CSVLINE在田里。

#默认情况下,输入一行被读出并解析成逗号分隔的多个领域。
#字段可以选择包含双引号的数据,包括字段分隔符。

#不同的字段分隔符可以用-d指定。您可以使用-D
#改为记录的定义(如支持NULL分隔的记录)。

csvline(){
    CSVLINE =()
    本地线路报价场= 0 =分隔符,lineDelimiter = $的'\\ n'C
    当地OPTIND = 1 ARG
    而getopts的:D:ARG;做
        案例$ ARG在
            D)分隔符= $ OPTARG ;;
        ESAC
    DONE    IFS =读-d$ lineDelimiter-r行||返回
    而IFS =读-rn1℃;做
        案例$ C在
            \\)
                ((报价=!援引))
                继续;;
            $分隔符)
                如果((引)!);然后
                    CSVLINE + =($场)字段=
                    继续
                网络;;
        ESAC
        场+ = $ C
    完成<<< $线
    [[$场]&放大器;&安培; CSVLINE + =($场)||:
}#_____________________________________________________________________

I'm trying to parse a .csv file and I have some problems with IFS. The file contains lines like this:

"Hello","World","this","is, a boring","line"

The columns are separated with a comma, so I tried to explode the line with this code:

IFS=, read -r -a tempArr <<< "$line"

But I get this output:

"Hello"
"World"
"this"
"is
a boring"
"line"

I understand why, so I tried some other commands but I don't get my expected output.

IFS=\",\"
IFS=\",
IFS=',\"'
IFS=,\"

Every time the third element is seperated in 2 parts. How can I use IFS to seperate the string to 5 parts like this?

"Hello"
"World" 
"this" 
"is, a boring" 
"line"

解决方案

bashlib provides a csvline function. Assuming you've installed it somewhere in your PATH:

line='"Hello","World","this","is, a boring","line"'

source bashlib
csvline <<<"$line"
printf '%s\n' "${CSVLINE[@]}"

...output from the above being:

Hello
World
this
is, a boring
line


To quote the implementation (which is copyright lhunath, the below text being taken from this specific revision of the relevant git repo):

#  _______________________________________________________________________
# |__ csvline ____________________________________________________________|
#
#       csvline [-d delimiter] [-D line-delimiter]
#
# Parse a CSV record from standard input, storing the fields in the CSVLINE array.
#
# By default, a single line of input is read and parsed into comma-delimited fields.
# Fields can optionally contain double-quoted data, including field delimiters.
#
# A different field delimiter can be specified using -d.  You can use -D
# to change the definition of a "record" (eg. to support NULL-delimited records).
#
csvline() {
    CSVLINE=()
    local line field quoted=0 delimiter=, lineDelimiter=$'\n' c
    local OPTIND=1 arg
    while getopts :d: arg; do
        case $arg in
            d) delimiter=$OPTARG ;;
        esac
    done

    IFS= read -d "$lineDelimiter" -r line || return
    while IFS= read -rn1 c; do
        case $c in
            \")
                (( quoted = !quoted ))
                continue ;;
            $delimiter)
                if (( ! quoted )); then
                    CSVLINE+=( "$field" ) field=
                    continue
                fi ;;
        esac
        field+=$c
    done <<< "$line"
    [[ $field ]] && CSVLINE+=( "$field" ) ||:
} # _____________________________________________________________________

这篇关于世界&QUOT;,&QUOT; IFS分离像&QUOT的字符串;你好&QUOT; QUOT,与此&QUOT;,&QUOT是,一个镗QUOT;,&QUOT;行&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆