基于空行或任何未使用的字符将文本文件拆分为数组

Blu*_*luz 5 arrays bash ascii echo ifs

我有一个文本文件,其中包含由空行文本分隔的文本行.我想将该文件的内容推送到一个数组中,并使用空行作为分隔符.我尝试过IFS ="\n"(或"\ r \n"等等),但无法让它工作,所以相反我以为我会用不在文件中的字符替换任何空行,所以我拿起西班牙语倒置问号(\ xBF)

sed 's/^$/'$(echo -e "\xBF")'/'))
Run Code Online (Sandbox Code Playgroud)

所以这有用,我有一个角色,我将用它来切片我的文件并把它放入一个数组.(一点随机技巧,但嘿,这只是一种方式做到这一点..)

现在我需要更改$ IFS,因此它将使用反转的问号来切割数组的数据.

如果我输入

IFS=$(echo -e "\xBF")
Run Code Online (Sandbox Code Playgroud)

在命令行中它工作得很好

 echo "$IFS"
¿
Run Code Online (Sandbox Code Playgroud)

但是如果我用尾随读取-a键入该命令,那么它什么都不做:

[user@machine ~]$ IFS=$(echo -e "\xBF") read -a array <<< "$var"
[user@machine ~]$ echo "$IFS"
[user@machine ~]$
Run Code Online (Sandbox Code Playgroud)

所以这很奇怪,因为$ var有一个值.

更令人惊讶的是,当我得到IFS之后立即验证IFS的价值时:

[user@machine ~]$ echo -n "$IFS" | od -abc
0000000  sp  ht  nl
    040 011 012
         \t  \n
0000003
[user@machine ~]$ 
Run Code Online (Sandbox Code Playgroud)

这是IFS的默认值.

我很确定IFS可以使用任何字符,不是吗?

或者,如果你有任何技巧可以将一个文件拆分成一个基于空行的分割,我很感兴趣!(为了理解起见,我仍然想深究这一点).

非常感谢,并有一个很好的周末:)

use*_*001 5

这个脚本应该做你想做的:

#!/bin/bash

i=1
s=1
declare -a arr
while read -r line 
do
    # If we find an empty line, then we increase the counter (i), 
    # set the flag (s) to one, and skip to the next line
    [[ $line == "" ]] && ((i++)) && s=1 && continue 

    # If the flag (s) is zero, then we are not in a new line of the block
    # so we set the value of the array to be the previous value concatenated
    # with the current line
    [[ $s == 0 ]] && arr[$i]="${arr[$i]}
$line" || { 
            # Otherwise we are in the first line of the block, so we set the value
            # of the array to the current line, and then we reset the flag (s) to zero 
            arr[$i]="$line"
            s=0; 
    }
done < file

for i in "${arr[@]}"
do
   echo "================"
   echo "$i"
done 
Run Code Online (Sandbox Code Playgroud)

测试文件:

$ cat file
asdf dsf s dfsdaf s
sadfds fdsa fads f dsaf as

fdsafds f dsf ds afd f saf dsf
sdfsfs dfadsfsaf

sdfsafds fdsafads fd saf adsfas
sdfdsfds fdsfd saf dsa fds fads f
Run Code Online (Sandbox Code Playgroud)

输出:

================
asdf dsf s dfsdaf s
sadfds fdsa fads f dsaf as
================
fdsafds f dsf ds afd f saf dsf
sdfsfs dfadsfsaf
================
sdfsafds fdsafads fd saf adsfas
sdfdsfds fdsfd saf dsa fds fads f
Run Code Online (Sandbox Code Playgroud)

更新:

为了忽略以 开头的行#,您可以在 之后添加这一行do

[[ $line =~ ^# ]] && continue
Run Code Online (Sandbox Code Playgroud)


tha*_*guy 4

首先,根据设计,设置的变量var=foo command仅适用于command脚本的其余部分,而不会为脚本的其余部分设置。

至于您的问题,read读取记录直到第一个分隔符(-d,默认:换行符),然后将其拆分为字段$IFS

要循环您的项目,您可以使用

sed -e 's/^$/\xBF/' | while read -d $'\xBF' var
do
    printf "Value: %s\n-----\n" "$var"
done
Run Code Online (Sandbox Code Playgroud)

要将它们从字符串中全部读入数组,您可以读取直到您希望没有的某个字符,例如 NUL 字节:

IFS=$'\xBF' read -d '' -a array <<< "$var"
Run Code Online (Sandbox Code Playgroud)