アイテムに改行を挿入するには?

アイテムに改行を挿入するには?

タブ区切りファイル 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

私のスクリプトは、区切り文字で区切られた2つの値の文字数が同じ場合に機能します。

任意の数の空白が必要な場合は、次のような結果が得られるため、通常は不可能です。

key1 12|1222 10|15

key1 12 10
key1 1222 15

この場合、その列はすでに「1222」で埋められているため、「15」を「10」の下の列に揃えることはできません。

編集する:私はコードを書き換えて、必要な数の行と必要な数のパイプを使用します:-)


パスワード:

スクリプト.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                 
-----------------------------------------------------------

おすすめ記事