您的位置:首页 > 教育 > 锐评 > 厦门免费做网站_贵州遵义疫情最新政策今天_网络营销技巧和营销方法_关键词推广怎么做

厦门免费做网站_贵州遵义疫情最新政策今天_网络营销技巧和营销方法_关键词推广怎么做

2025/5/9 12:41:04 来源:https://blog.csdn.net/weixin_43360707/article/details/144240844  浏览:    关键词:厦门免费做网站_贵州遵义疫情最新政策今天_网络营销技巧和营销方法_关键词推广怎么做
厦门免费做网站_贵州遵义疫情最新政策今天_网络营销技巧和营销方法_关键词推广怎么做

比如如下文本:

Timestamp                               : Sat Nov 16 18:28:46 2024 
Driver Version                          : 560.35.03 
CUDA Version                            : 12.560.35.03 
Attached GPUs                           : 3 
N/A                                     Product Name                        : NVIDIA H100 PCIe Product Brand                       : NVIDIA Product Architecture                : HOPPER compute_capability                  : 9.0 max resident grids per device       : 128 max resident blocks per sm          : 32 max resident warps per sm           : 64 Display Mode                        : Enabled Display Active                      : Disabled Persistence Mode                    : Enabled Addressing Mode   ..

我想转为json,

{
"Timestamp": "Sat_Nov_16_18:28:46_2024",
"Driver_Version": "560.35.03",
"CUDA_Version": "12.560.35.03",
"Attached_GPUs": "3",
"N/A": {
"Product_Name": "NVIDIA_H100_PCIe",
"Product_Brand": "NVIDIA",
"Product_Architecture": "HOPPER",
"compute_capability": "9.0",
"max_resident_grids_per_device": "128",
"max_resident_blocks_per_sm": "32",
"max_resident_warps_per_sm": "64",
"Display_Mode": "Enabled",
...
}
}

如下是脚本

#!/bin/bashinput_file="h100.txt"
output_file="output.json"indent=0stack=()
push() {stack+=("$1")
}
pop() {if [ ${#stack[@]} -eq 0 ]; thenecho "empty"elseunset 'stack[${#stack[@]}-1]'fi
}
peek() {if [ ${#stack[@]} -eq 0 ]; thenecho "empty"elseecho "${stack[${#stack[@]}-1]}"fi
}count_leading_whitespace() {local line="$1"local count=0while [[ "${line:$count:1}" == " " ]]; do((count++))doneecho $count
}last_key=""echo "{" > $output_filewhile IFS= read -r line; do# Skip empty linesif [[ -z "$line" ]]; thencontinuefiwhitespace_count=$(count_leading_whitespace "$line")last_line_whitespace_count=$(peek)if [ $last_line_whitespace_count == "empty" ]; then# push $whitespace_countlast_line_whitespace_count=$(peek)fiecho "whitespace_count: $whitespace_count"echo "last_line_whitespace_count: $last_line_whitespace_count"# Remove leading spaces and replace spaces between words with underscoresline=$(echo "$line" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]\+/_/g')echo "Line: $line"if [[ $whitespace_count -eq $last_line_whitespace_count ]]; thenif [[ "$line" =~ : ]]; thenkey=$(echo "$line" | cut -d: -f1  | sed 's/_$//g')value=$(echo "$line" | cut -d: -f2- | sed 's/^ *//' | sed 's/^_//g' | sed 's/_$//g')printf "\"%s\": \"%s\",\n" "$key" "$value" >> $output_fileelselast_key=$(echo "$line" | cut -d: -f1  | sed 's/_$//g')echo "Last key: $last_key"fiecho "first stage"elif [[ $whitespace_count -gt $last_line_whitespace_count ]]; thenpush $whitespace_countpushed=1echo "send stage, last key: $last_key"key=$(echo "$line" | cut -d: -f1  | sed 's/_$//g')value=$(echo "$line" | cut -d: -f2- | sed 's/^ *//' | sed 's/^_//g' | sed 's/_$//g')if [[ -n "$last_key" ]]; thenprintf "\"%s\": {\n" "$last_key" >> $output_filelast_key=""elsepopfiprintf "\"%s\": \"%s\",\n" "$key" "$value" >> $output_filelast_key=""echo "send stage"elif [[ $whitespace_count -lt $last_line_whitespace_count ]]; thenlast_key=$(echo "$line" | cut -d: -f1  | sed 's/_$//g')echo "Last key: $last_key"sed -i '$ s/,$//' $output_fileprintf "},\n"  >> $output_filepopfidone < "$input_file"while [[ ${#stack[@]} -gt 0 ]]; dosed -i '$ s/,$//' $output_fileprintf "}\n" >> $output_filepop
donesed -i '$ s/,$//' $output_file
printf "}\n" >> $output_file

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com