0

I use last | grep pts/ to check who login my raspberry Pi:

samuelli pts/2        localhost        Fri Apr 24 19:51   still logged in   
pi       pts/1        localhost        Fri Apr 24 19:50 - 19:53  (00:03)    
liuly    pts/1        localhost        Fri Apr 24 19:50 - 19:50  (00:00) 

Now I want to extract the user name like pi and their login time like Fri Apr 24 19:51 and output username and time into a file. How could I to write this script ? Using cut ?

1
  • What have you tried so far? The problem with cut is that it doesn't handle variable amounts of spaces well. Check out awk instead. Commented Apr 24, 2015 at 21:28

1 Answer 1

1

Using awk will print out the username as well as time and the >> can redirect it to a text file:

last | grep pts/ | awk '{print $1 " "$4 " "$5 " "$6 " "$7}' >> sampletest.txt

Sign up to request clarification or add additional context in comments.

1 Comment

One of the main features of an awk program is the condition that may precede the action block. It often makes sense to use that condition to select lines instead of an extra grep: last | awk ' /pts\// {print [...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.