最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux?shell實現(xiàn)折線圖代碼實例

 更新時間:2023年12月18日 15:28:19   作者:〖羊頭〗??lsy  
這篇文章主要給大家介紹了關(guān)于linux?shell實現(xiàn)折線圖的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家學習或者使用shell具有一定的參考借鑒價值,需要的朋友可以參考下

生成 100 行數(shù)據(jù)到文件 file.txt

for ((i=1; i<=100; i++));do
        if [ ${i} -lt 10 ];then
                num=$(( (RANDOM % 5) + 10 + i ))
        else
                num=$(( (RANDOM % 5) + 10 + 15 ))
        fi
        echo ${num}
done

通過文件 file.txt 生成折線圖

#!/bin/bash

# 指定文件名,該文件必須為數(shù)字
file='./file.txt'
# 指定折線圖的符號
symbol='@'
# 指定x軸最大長度
x_length=150
# 指定y軸高度
y_length=25

# 生成文件時需要的臨時文件(不用配置)
tmp_file='./.tmp.txt'
new_file='./.new.txt'

# 讀取文件行數(shù),如果為0則退出,如果小于x軸長度則更新x軸長度,如果大于x軸長度則按比例只取指定的長度
file_rows=$(wc -l < ${file})
if [ ${file_rows} -eq 0 ];then
        echo -e "[ERROR] If the file is empty, exit !"
        exit 1
elif [ ${file_rows} -le ${x_length} ];then
        x_length=${file_rows}
else
        residue=$((file_rows / x_length))
        awk "NR % ${residue} == 0" ${file} > ${new_file}
        file=${new_file}
fi

# 獲取最大值、最小值
max_number=$(awk 'NR==1 {max=$1} $1>max {max=$1} END{print max}' ${file})
min_number=$(awk 'NR==1 {min=$1} $1<min {min=$1} END {print min}' ${file})
# 獲取最大行的字符數(shù)
max_char=$(awk 'NR==1 {max=length} length>max {max=length} END{print max}' ${file})
# 如果最大行的字符數(shù)為偶數(shù),將第1列字符數(shù)+3,(為了避免對不齊的問題),+4是為了避免存在負數(shù)對不齊問題
[ $((max_char % 2)) -eq 0 ] && max_char=$((max_char + 3)) || max_char=$((max_char + 4))
x_symbol_length=$((x_length + max_char + 3))

# 輸出第一行圖表橫線字符
perl -E "say '—' x ${x_symbol_length}" >${tmp_file}

# 輸出第一行說明
title="scatter plot"
padding=$[(${x_length} - ${#title}) / 2]
printf "|%-${max_char}s|%-${padding}s%s%-${padding}s|\n" "unit" "" "${title}" "" >>${tmp_file}
perl -E "say '—' x ${x_symbol_length}" >>${tmp_file}

# 按指定文件的數(shù)字輸出單位長度
num=${max_number}
scale=$(awk "BEGIN{print int( (${max_number} - ${min_number}) / ${y_length} + 1)}")
for ((i=1; i<=y_length; i++));do
        printf "|%-${max_char}s|\n" "${num}" >>${tmp_file}
        num=$(awk "BEGIN{print int(${num} - ${scale})}")
done

# 橫向刻劃散點圖
x_length=$((x_length + 1))
for ((i=1; i<=x_length; i++));do
        num=${max_number}
        for ((j=1; j<=y_length; j++));do
                file_row=$((j+3))
                file_num=$(sed -n "${i}p" ${file} |awk "{print int(\$1 / ${num})}")
                if [[ ${i} == ${x_length} ]];then
                        sed_symbol='|'
                        result=0
                elif [[ ${file_num} == 1 ]];then
                        sed_symbol="${symbol}"
                        result=1
                else
                        sed_symbol=' '
                        result=0
                fi

                sed -i "${file_row}s/\$/${sed_symbol}/" ${tmp_file}

                if [[ ${result} == 1 ]];then
                        if [[ ${j} == ${y_length} ]];then
                                continue 2
                        else
                                sed_str="$((file_row+1)),$((y_length+3))"
                                sed -i "${sed_str}s/\$/ /" ${tmp_file}
                                continue 2
                        fi
                fi
                num=$(awk "BEGIN{print int(${num} - ${scale})}")
        done
done

# 輸出最后一行圖表橫線字符
perl -E "say '—' x ${x_symbol_length}" >>${tmp_file}
cat ${tmp_file}

[ -f ${tmp_file} ] && rm -rf ${tmp_file}
[ -f ${new_file} ] && rm -rf ${new_file}

總結(jié) 

到此這篇關(guān)于linux shell實現(xiàn)折線圖的文章就介紹到這了,更多相關(guān)shell實現(xiàn)折線圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

朝阳县| 会宁县| 滁州市| 汉源县| 建平县| 巍山| 磐石市| 郯城县| 英德市| 云阳县| 拜城县| 巴青县| 蓬溪县| 屯昌县| 兴国县| 新密市| 苏尼特左旗| 定日县| 黄冈市| 康保县| 象州县| 黎川县| 罗平县| 集安市| 海门市| 共和县| 通榆县| 罗田县| 永修县| 梓潼县| 子长县| 长子县| 青神县| 辽阳县| 怀化市| 桂东县| 深水埗区| 开原市| 阿勒泰市| 甘孜县| 永福县|