Bash text file editing/modifying

后端 未结 2 420
北海茫月
北海茫月 2021-01-27 18:52

I have a text file that I am trying to modify. I am taking the input file that has lines of the form of

(y+1/4,-x+1/2,z+3/4)

and trying to chan

2条回答
  •  伪装坚强ぢ
    2021-01-27 19:32

    Here's an approach that may simplify the parsing. Read each line into an array using IFS set to all possible delimiters and characters you don't care about:

    while IFS=$'\(\)+,' read -ra line; do
        for i in 1 3 5; do
            case "${line[$i]}" in
                x) printf "%s\t%s\t%s\t" 1 0 0 ;;
                y) printf "%s\t%s\t%s\t" 0 1 0 ;;
                z) printf "%s\t%s\t%s\t" 0 0 1 ;;
                -x) printf "%s\t%s\t%s\t" -1 0 0 ;;
                -y) printf "%s\t%s\t%s\t" 0 -1 0 ;;
                -z) printf "%s\t%s\t%s\t" 0 0 -1 ;;
            esac
        done
        for i in 2 4 6; do
            printf "%s\t" "${line[$i]}"
        done
        echo
    done < "$filename"
    

提交回复
热议问题