Ubuntu - 查看数据行 10 by 10

Ubuntu - 查看数据行 10 by 10

我需要编写一个脚本来查看 /etc/passwd 10 x 10 的页码。因此按下按钮后它将显示接下来的 10 行。我应该使用什么命令?尝试检查 awk、head、tail、命令,但没有找到解决方案。

#!/bin/bash 

awk -F":" 'BEGIN { print "--------------------------------------------------"
                    printf "%-20s %-10s %-3s\n","USERNAME","GUID","DIRECTORY" 
                    print "--------------------------------------------------" 
                 } 
           NR==1, NR==10 {printf "%-20s %-10s %-3s\n", $1, $4, $7} ' /etc/passwd 

答案1

getline rc < "-"每第 10 行添加一次。要抑制换行符,请使用以下命令将光标向上移动一行system("tput cuu1")

awk -F":" '
    BEGIN { print "--------------------------------------------------"
            printf "%-20s %-10s %-3s\n","USERNAME","GUID","DIRECTORY" 
            print "--------------------------------------------------" }
    !(NR%10) { getline rc < "-"; system("tput cuu1") }
    { printf "%-20s %-10s %-3s\n", $1, $4, $7 }
' /etc/passwd

要添加页码,您可以使用:

awk -F":" -v TNR=$(wc -l < /etc/passwd) '
    BEGIN { NUM_PAGES=TNR/10+1
            print "--------------------------------------------------"
            printf "%-20s %-10s %-3s\n","USERNAME","GUID","DIRECTORY" 
            print "--------------------------------------------------" }

    !(NR%10) { PAGE++
               printf "--- Page %d of %d ---",PAGE,NUM_PAGES
               getline rc < "-"; system("tput cuu1") }

    { printf "%-20s %-10s %-3s\n", $1, $4, $7 }
' /etc/passwd

相关内容