【问题标题】:Take input via user in order to execute a last and cut command通过用户输入以执行最后一个和剪切命令
【发布时间】:2012-10-23 06:22:07
【问题描述】:

我想提示用户从“最后一个”命令中剪切以及要剪切的列。我还想使用 '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)    

【问题讨论】:

  • 您希望输出是什么样的?能举个例子吗?

标签: bash shell scripting cut tr


【解决方案1】:

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

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}'

【讨论】:

  • 这就是我要找的。我应该首先使用 awk 。谢谢。
【解决方案2】:

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

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 命令我特别困惑。你想用它做什么?

【讨论】:

  • 我只是想用'tr'去掉非打印字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多