如何将条目插入换行符?

如何将条目插入换行符?

我在制表符分隔文件 test1.txt 中有以下条目

key1 10|15 20 30 11|xx 21 31
key2 527|240 269 0 0 0 2462 546 281 0 0

有什么办法可以将此文件转换为以下格式吗?我想将管道分隔值带入下一行。可以有任意数量的管道,并且由管道分隔的值必须具有相同数量的字符。

key1 10 20 30 11 21 31
key1 15       xx    
key2 527 269 0 0 0 2462 546 281 0 0
key2 240  

答案1

如果由分隔符分隔的两个值具有相同数量的字符,则我的脚本有效。

如果您想要任意数量的空格,通常是不可能的,因为您可能会遇到这样的情况:

key1 12|1222 10|15

key1 12 10
key1 1222 15

在这种情况下,您根本无法将“15”对齐到“10”下的列,因为该列已被“1222”占用。

编辑:我重写了代码,因此它需要任意数量的行,甚至也需要任意数量的管道:-)


代码:

脚本.sh:

#!/bin/bash

# count max number of pipes -> recognizes number of lines
countPipes() {
    num=0
    line="$@"
    IFS=' '
    for item in $line; do
        tmp=$(echo "$item" | tr -d "[:alnum:][:space:]" | wc -c)

        if [[ "$num" < "$tmp" ]]; then
            num=$tmp
        fi
    done

    return $num
}

makeLines() {
    strTmp="$@" # one line from input file

    arrTmp=($strTmp)
    key=${arrTmp[0]}
    arrIN=$key

    # create output arrays (one array = one output line)
    countPipes $strTmp
    maxPipeCount=$? # assign the value of the last 'return'

    for((i=0;i<$maxPipeCount;++i)); do
        arrOUT[$i]="$key"
    done

    for item in ${strTmp[@]}; do
        # Delimiter handling
        if [[ $item == *\|* ]]; then # If there is a pipe
            IFS='|'
            tmp=($item) # variable containing pipe character -> split by space -> array
            IFS=' '

            arrIN="$arrIN ${tmp[0]}"

            for ((i=0;i<"${#arrOUT[@]}";++i)); do # fill columns in every output line - i = index in line
                if [[ "${#tmp[$(($i + 1))]}" -gt 0 ]]; then
                    arrOUT[$i]="${arrOUT[$i]} ${tmp[$(($i + 1))]}"
                else
                    # Handling spaces in output
                    for ((j=0;j<="${#tmp[0]}";++j)); do # filling line with spaces - j = just counter, not index
                        arrOUT[$i]="${arrOUT[$i]} "
                    done
                fi
            done

        elif [[ "$item" != "$key" ]]; then # If there isn't a pipe
            arrIN="$arrIN $item"

            # Handling spaces in output
            for ((i=0;i<"${#arrOUT[@]}";++i)); do # for every output line
                for j in { 1.."${#tmp[0]}" }; do # for every char in $item
                    arrOUT[$i]="${arrOUT[$i]} "
                done
            done
        fi
    done

    # PRINT RESULTS
    echo "$arrIN"

    for((i=0;i<"${#arrOUT[@]}";++i)); do # for every output line
        echo "${arrOUT[$i]}"
    done
    unset arrOUT

    echo '-----------------------------------------------------------'
}

while read line; do # load data from STDIN
    makeLines $line
done

例子:

测试.txt:

key1 10|15 20 30 11|XX|55 21 31|20 100
key2 11 25|30 58|22 44 33
key3 12|15|17|19 22 33 55|22 88|44|11 xxxx|2222|7777

命令:

bash ./script.sh < test.txt 

输出:

key1 10 20 30 11 21 31 100
key1 15       XX    20   
key1          55         
-----------------------------------------------------------
key2 11 25 58 44 33
key2    30 22      
-----------------------------------------------------------
key3 12 22 33 55 88 xxxx
key3 15       22 44 2222
key3 17          11 7777
key3 19                 
-----------------------------------------------------------

相关内容