key1=value1|key2=value2|key3=value3
key1=value1|key2=value2|key3=value3
key1=value1|key2=value2|key4=value4
key1=value1|key2=value2|key5=value5
key1=value1|key2=value2|key3=value3
至(带分隔符的文本文件)
key1|key2|key3|key4|key5
value1|value2|value3||
value1|value2|value3||
value1|value2||value4|
value1|value2|||value5
value1|value2|value3||
答案 0 :(得分:0)
如果可以使用bash,请尝试以下操作:
#!/bin/bash
# define headline
headline="key1|key2|key3|key4|key5"
# split headline into separat entries
readarray -d '|' header <<< "$headline"
# get last fieldnr in array
lastfieldnr=$((${#header[@]} - 1))
# write headline to output
echo "$headline"
# loop over all entry lines
while read -r line; do
# separate key=value pairs
readarray -d '|' key_value <<< "$line"
# define associative array
declare -A content
# loop over key=value pairs
for kv in "${key_value[@]}"; do
# split key value pair in separate fields
# strip last |
IFS='=' read -r key value <<< "${kv%|}"
# save result in associative array
content[$key]="$value"
done
# initialize output
output=""
# loop over all fields
for i in $(seq 0 $lastfieldnr); do
# get length of key
length="${#header[$i]}"
# strip last character from key
key="${header[$i]:0:$length - 1}"
# if content[$key] not empty
if [ -n "${content[$key]}" ]; then
# get value
value="${content[$key]}"
# append value to output
output+="$value"
fi
# append | at end of output
output+="|"
done
# complete line to output, strip last |
printf "%s\n" "${output%|}"
# delete associateive array
unset content
done < file.log
输入(file.log)
key1=value1|key2=value2|key3=value3
key1=value1|key2=value2|key3=value3
key1=value1|key2=value2|key4=value4
key1=value1|key2=value2|key5=value5
key1=value1|key2=value2|key3=value3
输出
key1|key2|key3|key4|key5
value1|value2|value3||
value1|value2|value3||
value1|value2||value4|
value1|value2|||value5
value1|value2|value3||