Pregunta

I want to prompt a user to cut from a 'last' command along with what columns to cut. I also want to use 'tr' to fix all the difference in white spaces. This is the code I have:

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

When I run it from the command line I get the prompt, so I hit enter then put in a number to cut. I then hit enter again and I get a list of info that prints to screen but doesn't break out of the executed script. Is there a better way to set this script up? Here's some sample data of what it echo's out to the screen:

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)    
¿Fue útil?

Solución

Is doing this fits your needs ?

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

Otros consejos

I don't really understand what you're trying to do here. But I'm pretty sure you want to pipe those commands to each other so they operate on each other's output:

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

last $num_int probably doesn't do what you want. last expects usernames as arguments. To get the last $num_int lines of output, try tail (or maybe head):

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

The tr command I'm particularly confused about. What are you trying to do with that?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top