Awk consider double quoted string as one token and ignore space in between

前端 未结 7 966
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 17:26

Data file - data.txt:

ABC \"I am ABC\" 35 DESC
DEF \"I am not ABC\" 42 DESC

cat data.txt | awk \'{print $2}\'

will re

7条回答
  •  时光说笑
    2020-12-15 18:13

    Here is something like what I finally got working that is more generic for my project. Note it doesn't use awk.

    someText="ABC \"I am ABC\" 35 DESC '1 23' testing 456"
    putItemsInLines() {
        local items=""
        local firstItem="true"
        while test $# -gt 0; do
            if [ "$firstItem" == "true" ]; then
                items="$1"
                firstItem="false"
            else
                items="$items
    $1"
            fi
            shift
        done
        echo "$items"
    }
    
    count=0
    while read -r valueLine; do
        echo "$count: $valueLine"
        count=$(( $count + 1 ))
    done <<< "$(eval putItemsInLines $someText)"
    

    Which outputs:

    0: ABC
    1: I am ABC
    2: 35
    3: DESC
    4: 1 23
    5: testing
    6: 456
    

提交回复
热议问题