Using tail with the filter command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Devise a chain of filters using some or all of the following programs pr, cut, cat, tail to
    display a numbered list of last (in the order of appearance) 7 entries from /etc/passwd file.
    Each entry of the list must contain 4 fields: login name, numerical user ID, numerical
    group ID, and user home directory separated by ---. For example:
1 stud25---1025---1001---/home/students/stud25
2 stud26---1026---1001---/home/students/stud26
3 stud27---1027---1001---/home/students/stud27
4 stud28---1028---1001---/home/students/stud28
5 stud29---1029---1001---/home/students/stud29
6 stud30---1030---1001---/home/students/stud30
7 stud50---1031---1001---/home/students/stud50
  1. Relevant commands, code, scripts, algorithms:
tail/pr/cut/cat
  1. The attempts at a solution (include all code and scripts):
cut | pr -tn | etc/passwd tail - 7
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Kennesaw State Univ. Atlanta GA, USA DR. Shah CSCI 3100

Personally I can't see a way of achieving the required result with just standard unix versions of tail/pr/cut/cat . Other posters may have better ideas. Maybe you have a non-unix Operating System with extended versions of these standard commands?

To get your pipeline started (and to avoid any code which might overwrite the /etc/passwd file!), let's simplify the problem by reading only the last 7 lines of /etc/passwd and outputting only the fields we require:

tail -7 /etc/passwd | cut -d':' -f1,3,4,6

The output is however still separated with colon characters.
You now need to build the pipeline to format the output and number the lines.

Avoiding awk and sed programs which will no doubt come up later on your course, personally I'd still need a unix command like printf (or maybe tr ?) to sort out the colon characters).

Numbering the lines could be achieved with something like pr -n1 -t or perhaps just cat -n .

Again the unix printf command would be really useful for sorting out the final format including prettifying the output with hyphen characters.

Footnote: If the tail/pr/cut/cat is just a hint not a rule and you are allowed to set up a while read loop and use the echo command and use Shell arithmetic to set up a simple record counter ... it becomes much easier.
Well worth querying with your lecturer if you are allowed to use commands and techniques already taught ... as well as a selection from the suggested commands.

Hi hbell221,

The command explains by itself:

$ tail -7 /etc/passwd | cut -d: --output-delimiter=--- -f1,3-4,7 | pr -tn --indent=2
      1 birei---1000---1000---/bin/bash
      2 hal---82---82---/bin/false
      3 mysql---89---89---/bin/false
      4 kdm---135---135---/bin/false
      5 avahi---84---84---/bin/false
      6 usbmux---140---140---/sbin/nologin
      7 mongodb---999---2---/bin/bash

Hi bieri, thank you for your reply. Can you explain step by step how you came to that command? I'm just trying to get a better understanding of the process involved in using these commands instead of just getting the answer.

@hbell221
What Operating System and version are you running?

@birei
What Operating System and version are you running?
It's clearly not unix.

@hbell221:

Of course:

tail -7 /etc/passwd ---> Get last seven lines from file /etc/passwd
cut -d: --output-delimiter=--- -f1,3-4,7 ---> Split line with colon, select fields 1, 3, 4 and 7, and print them separated by '---'
pr -tn --indent=2 ---> You had used it in your attempt: omit header, print line number and the indent was an attempt to print numbers near the text.

@methyl:

Arch Linux.

$ uname -mosr
Linux 3.3.4-1-ARCH i686 GNU/Linux

Thank you @birei. All we need to know now is whether @hbell221 has an advanced Linux ... not unix. Most of the course posters we see here are being taught Bourne Shell on unix systems.

Oh sorry for not clarifying that early but after typing

uname -mosr

in my putty program I get

Linux 2.6.32-5-amd64 x86_64 GNU/Linux

Not sure if this matters but my operating system is Windows. I'm guessing it's just saying Linux because I'm in the putty program?

---------- Post updated 05-05-12 at 05:20 AM ---------- Previous update was 05-04-12 at 09:28 PM ----------

---------- Post updated at 05:20 AM ---------- Previous update was at 05:20 AM ----------

Should it be saying the OS is Linux if I'm using Windows?

The operating system on which you run the utilities and where those data files are stored is Linux. You are using putty on a Windows system to get there, but that is not relevant here.