0

我想提示用户从“最后一个”命令中剪切以及要剪切的列。我还想使用 'tr' 来修复空白处的所有差异。这是我的代码:

echo -n "What rows and columns would you like to cut: "
read num_int
read num_row
read num_list
last $num_int
cut -f $num_row
tr -d $num_list

当我从命令行运行它时,我得到了提示,所以我按回车键,然后输入一个数字来剪切。然后我再次按回车键,我得到一个打印到屏幕但不会脱离已执行脚本的信息列表。有没有更好的方法来设置这个脚本?这是它在屏幕上回显的一些示例数据:

slater   pts/2        78.189.121.247   Sat Sep  1 23:21 - 23:27  (00:05)    
slater   pts/3        77.189.121.247   Sat Sep  1 23:09 - 23:21  (00:12)    
slater   pts/2        76.189.121.247   Sat Sep  1 22:59 - 23:09  (00:10)    
slater   pts/2        74.189.121.247   Sat Sep  1 22:51 - 22:56  (00:05)    
pint     pts/2        74.189.121.247   Sat Sep  1 22:49 - 22:51  (00:01)    
terry    pts/2        74-119-247-134.r Sat Sep  1 19:08 - 19:09  (00:00)    
4

2 回答 2

2

这样做是否符合您的需求?

read -p "What row and column integers would you like to cut >>> " col row
 last | awk -v col=$col -v row=$row 'NR==row{print $col}'
于 2012-10-22T22:15:59.313 回答
0

我真的不明白你想在这里做什么。但我很确定您想将这些命令相互传递,以便它们对彼此的输出进行操作:

last | cut -f $num_row | tr -d $num_list

last $num_int可能不会做你想做的事。last期望用户名作为参数。要获得最后$num_int几行输出,请尝试tail(或者可能head):

last | tail -n $num_int | cut -f $num_row | tr -d $num_list

tr我特别困惑的命令。你想用它做什么?

于 2012-10-22T22:13:07.543 回答