Skip to main content
add example of shorter variant (shorter still)
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

$ grep --countc slm lastloggedin
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

$ grep --count slm lastloggedin
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

$ grep -c slm lastloggedin
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758 

$ grep --count slm lastloggedin
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758 

$ grep --count slm lastloggedin
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of recordsusing awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin

To get you started you can use awk to search for lines in a file that contain a string like so:

$ awk '/CFS264/ { .... }' lastloggedin

The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the search string.

$ awk '/CFS264/ { print $0 }' lastloggedin

As to the counting, if you search for "awk counter" you'll stumble upon this SO Q&A titled: using awk to count no of records. The method shown there would suffice for what you describe:

$ awk '/CFS264/ {count++} END{print count}' lastloggedin

Example

$ last > lastloggedin

$ awk '/slm/ {count++} END {print count}' lastloggedin 
758

$ grep slm lastloggedin  | wc -l
758

NOTE: You don't say which field CFS264 pertains to in the last output. Assuming it's a username then you could further restrict the awk command to search only that field like so:

$ awk '$1=="CFS264" { print $0 }' lastloggedin
edited body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
added 242 characters in body
Source Link
slm
  • 379.7k
  • 127
  • 793
  • 897
Loading
Source Link
slm
  • 379.7k
  • 127
  • 793
  • 897
Loading