
我有以下輸出:
2015/1/7 8
2015/1/8 49
2015/1/9 40
2015/1/10 337
2015/1/11 11
2015/1/12 3
2015/1/13 9
2015/1/14 102
2015/1/15 62
2015/1/16 10
2015/1/17 30
2015/1/18 30
2015/1/19 1
2015/1/20 3
2015/1/21 23
2015/1/22 12
2015/1/24 6
2015/1/25 3
2015/1/27 2
2015/1/28 16
2015/1/29 1
2015/2/1 12
2015/2/2 2
2015/2/3 1
2015/2/4 10
2015/2/5 13
2015/2/6 2
2015/2/9 2
2015/2/10 25
2015/2/11 1
2015/2/12 6
2015/2/13 12
2015/2/14 2
2015/2/16 8
2015/2/17 8
2015/2/20 1
2015/2/23 1
2015/2/27 1
2015/3/2 3
2015/3/3 2
我想畫直方圖
2015/1/7 ===
2015/1/8 ===========
2015/1/9 ==========
2015/1/10 ====================================================================
2015/1/11 ===
2015/1/11 =
...
你知道有沒有 bash 指令可以讓我這樣做嗎?
答案1
在perl
:
perl -pe 's/ (\d+)$/"="x$1/e' file
e
導致對表達式進行求值,因此我會重複使用(與 相符的數字)=
的值。$1
(\d+)
- 你可以做
"="x($1\/3)
而不是"="x$1
縮短線路。 (/
由於我們正處於替換命令的中間,因此被轉義。)
在bash
(靈感來自這個答案):
while read d n
do
printf "%s\t%${n}s\n" "$d" = | tr ' ' '='
done < test.txt
printf
$n
使用空格填充第二個字串以獲得( )的寬度%${n}s
,然後我將空格替換為=
。- 列使用製表符 (
\t
) 分隔,但您可以透過管道連接到 使其更美觀column -ts'\t'
。 - 您可以使用
$((n/3))
代替來${n}
縮短線路。
另一個版本:
unset IFS; printf "%s\t%*s\n" $(sed 's/$/ =/' test.txt) | tr ' ' =
sed
我看到的唯一缺點是,如果你想縮小規模,你需要將 的輸出透過管道傳輸到某個地方,否則這是最乾淨的選擇。如果您的輸入檔有可能包含其中之一,[?*
則應使用 w/ 引導命令set -f;
。
答案2
試試這個珀爾:
perl -lane 'print $F[0], "\t", "=" x ($F[1] / 5)' file
說明:
-a
是一個顯式split()
的@F
數組,我們得到的值是$F[n]
x
就是告訴perl印一個字元N次($F[1] / 5)
:在這裡我們得到數字並將其除以 5 以獲得漂亮的列印輸出(簡單的算術)
答案3
輕鬆搭配awk
awk '{$2=sprintf("%-*s", $2, ""); gsub(" ", "=", $2); printf("%-10s%s\n", $1, $2)}' file
2015/1/7 ========
2015/1/8 =================================================
2015/1/9 ========================================
..
..
或用我最喜歡的程式語言
python3 -c 'import sys
for line in sys.stdin:
data, width = line.split()
print("{:<10}{:=<{width}}".format(data, "", width=width))' <file
答案4
$ mlr --nidx --repifs --ofs tab bar -f 2 file
2015/1/7 ***.....................................
2015/1/8 *******************.....................
2015/1/9 ****************........................
2015/1/10 ***************************************#
2015/1/11 ****....................................
2015/1/12 *.......................................
.
.
.