gayavat
gayavat

Reputation: 19418

How to convert output with hexadecimal data to unicode?

I have file with hex symbols like below

cat demo 
\x22count\x22
\x22count\x22
\x22count\x22

I need conversion like:

echo $(cat demo)
"count" "count" "count"

How to get unicode symbols in pipeline with newline symbol? Something like:

cat demo | ???

"count" 
"count" 
"count"

Upvotes: 0

Views: 98

Answers (2)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

You can use printf's %b conversion specification. This will print the output you want:

printf '%b\n' "$(<demo)"

Note: %b causes printf to expand other backslash escape sequences as well (e.g., \n, \t etc.)

Upvotes: 1

Cole Tierney
Cole Tierney

Reputation: 10324

You could use printf to convert the hexadecimal data. Depending on the size of your input, you could read the lines into an array then use IFS to delimit the output:

join() {
    local IFS="$1"
    shift
    echo "$*"
}
arr=( $(while read -r line; do printf "$line "; done < demo) )

join $' ' "${arr[@]}"
"count" "count" "count"

join $'\n' "${arr[@]}"
"count"
"count"
"count"

Upvotes: 1

Related Questions