Using tail with the filter command

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Using tail with the filter command
# 1  
Old 05-04-2012
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:
Code:
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

2. Relevant commands, code, scripts, algorithms:
Code:
tail/pr/cut/cat

3. The attempts at a solution (include all code and scripts):
Code:
cut | pr -tn | etc/passwd tail - 7

4. 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

Last edited by methyl; 05-04-2012 at 07:47 PM.. Reason: Please use code tags ; correct some typos for readability
# 2  
Old 05-04-2012
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:
Code:
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.

Last edited by methyl; 05-04-2012 at 07:44 PM..
# 3  
Old 05-04-2012
Hi hbell221,

The command explains by itself:
Code:
$ 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

# 4  
Old 05-04-2012
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.
# 5  
Old 05-04-2012
@hbell221
What Operating System and version are you running?

@birei
What Operating System and version are you running?
It's clearly not unix.
# 6  
Old 05-04-2012
@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.
Code:
$ uname -mosr
Linux 3.3.4-1-ARCH i686 GNU/Linux


Last edited by methyl; 05-04-2012 at 07:59 PM.. Reason: Separate commands from text with icode tags.
# 7  
Old 05-04-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Tail -f Command help

Hi Team, Can anyone help me here: I have to access server logs via putty and these logs file is a trailing file (continously updating) with ERROR and WARNINGS... I need to know if I can pull this trailing file to a local drive so that I can do some higlighting on some keywords through Notepad... (13 Replies)
Discussion started by: jitensetia
13 Replies

2. How to Post in the The UNIX and Linux Forums

Usage of tail command in .awk

Hi, I want to do file format using awk script, for that i wan to use 'tail'. Here is the scenario. I will be having set of files in a directory. Those files i need to write to another directory with same file name, but while writing the file to out directory, i need to write the last line as... (3 Replies)
Discussion started by: Venkata Madhu
3 Replies

3. Shell Programming and Scripting

tail command help

Hi does anyone know how to create a file using the tail command? My book has this file I need to create and it says to use the tail command and that it is possible but I have no idea. Thanks. (4 Replies)
Discussion started by: drew211
4 Replies

4. Shell Programming and Scripting

tail command problems

Hi, In my home directory, there are so many files. i tried to get the lastly created file by following command. file=`ls -lrt MXOfiles* | tail -1` As there are so many files, it displays "$ : 0403-027 The parameter list is too long." Can someone tell me how can i get the recent file... (1 Reply)
Discussion started by: JSKOBS
1 Replies

5. UNIX for Advanced & Expert Users

How to alert if no output from tail -f command

Hi Dears, I am using AWK script to monitor some log files. My awk will get the input from the output of tail -f LOGFILE. (tail -f LOGFILE | myawkanalyzer) My doubt is, How can I notify, if I didn't get any output from the tail -f command. i.e) Log file stuck for some reason. I want to display... (1 Reply)
Discussion started by: sharif
1 Replies

6. Shell Programming and Scripting

Help with tail command

Hi All, My query seems to be silly but Iam unable to find where the exact problem lies. I have a script to unzip set of files here is the script #!/bin/ksh Count=`cat /home/gaddamja/Tempfile | wc -l` while do Filename=`cat /home/gaddamja/Tempfile |tail -$Count | head -1` cd... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

7. Solaris

Tail command in one line

HI i have to copy the last 5000 lines form a log file and copy the same in the same file .overwriting the same log file. ex: tail -5000 testfile1 > testfile2 cat testfile2 mv tesftfile2 testfile1 will produce the correct result.but i want to have this done in one line???? (4 Replies)
Discussion started by: saurabh84g
4 Replies

8. Shell Programming and Scripting

tail command..

I was wondering how can I do this I have file myfile.txt wc -l is: 5 000 000 I have to remove first 1 000 000 lines from header.. I tryed with tail -4000000 myfile.txt>newfile.txt but it does not work... any help?? (2 Replies)
Discussion started by: amon
2 Replies

9. Shell Programming and Scripting

tail command

Hi , I have found a interesting thing about tail command: when I tried to use 'tail -1 *' to look at every file with the current derectory, I only got one line of result of one file. But if I use 'head -1 *', I would get multiple lines. Is there a way to do get multiple lines with 'tail -1 *'... (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Advanced & Expert Users

help with !(tail -2) command.. using pipes

I am trying to using pipe (|) with ! (not) operator to list all the other files except the latest two and I am using the following command. $ ls -ltr *.lst|!(tail -2) ksh: 20050211180252.lst: cannot execute but it is trying to execute the file returned by tail -2. I am able to do that in 4... (8 Replies)
Discussion started by: sdlayeeq
8 Replies
Login or Register to Ask a Question