Command combination for displaying header and content


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Command combination for displaying header and content
# 1  
Old 08-19-2014
Command combination for displaying header and content

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:
You are given a ANSI text file (a.txt) in the following format and with the following contents-:

Code:
NAME ROLL MARKS 
Sam  05      80 
John 06      90 
Mark 07      90

Columns are separated with just one space. That means that there is only one space separating name from roll and one space separating roll from marks. There may be multiple spacing in the data as seen in the last column of marks.

The objective is to print the header information and then the details of JOHN only.

The output will look like the following-:

Code:
NAME ROLL MARKS 
John 06      90

You cannot use two separate commands by using the ";" format to write both commands. You must use pipes and redirection to combine filter commands to get the desired output.
2. Relevant commands, code, scripts, algorithms:

All filter commands can be used. The resultant command must work on a BASH shell and on any Linux distro.

3. The attempts at a solution (include all code and scripts):

Well I have so far attempted two tries-:

Code:
head -1 a.txt ; head -3 a.txt | tail -1

But I can't do this even though it works Smilie because of the restriction in the problem.

Code:
head -1 a.txt | tee /dev/tty | tail

this does not work and the reason is that after running the head command I get only one line and that is printed (which will be header since it is the first line). But I am unable to get the header and the particular record.

Is there someway I can filter within the tee command ?

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
College-: Techno India, Salt Lake
University: West Bengal University of Technology.
City: Kolkata (West Bengal)
Country: India
Professor: Mr. Arun Chatterjee
Course Number-: MCA302
Link to University- :
wbut.ac.in (Course 10 is computer applications MCA)
Link to college syllabus-:
MCA syllabus (Page 3 of the pdf will contain MCA302 as a listed subject)
Link to college-:
::Techno India Saltlake::

If this information above contains any discrepancy please don't delete this post just tell me and I will try my best to supply the information required.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 08-19-2014
It might also be worth the time to consult the manual page of sed: suppress default printing and selectively print the desired lines.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 08-20-2014
Thank you everyone who helped me here. I am asking is there a way to do this with head and tail commands ?

---------- Post updated at 09:57 PM ---------- Previous update was at 09:55 PM ----------

Quote:
Originally Posted by bakunin
It might also be worth the time to consult the manual page of sed: suppress default printing and selectively print the desired lines.
Well sir I am a total beginner to the commands. So I really have no idea what you mean. If you could give an example for some more info it would really be helpful. Thanks for replying though.
# 4  
Old 08-20-2014
Quote:
Originally Posted by sreyan32
Thank you everyone who helped me here. I am asking is there a way to do this with head and tail commands ?

---------- Post updated at 09:57 PM ---------- Previous update was at 09:55 PM ----------
Quote:
Originally Posted by bakunin
It might also be worth the time to consult the manual page of sed: suppress default printing and selectively print the desired lines.

Well sir I am a total beginner to the commands. So I really have no idea what you mean. If you could give an example for some more info it would really be helpful. Thanks for replying though.
No sir. We are not here to do your homework for you. (And those who posted full solutions to your homework problem are subject to infractions for giving inappropriate responses to homework questions.) The suggestion bakunin made is well worth your attention. We all assume that you should know about the man utility and that the command:
Code:
man sed

would give you guidance on how to use the sed utility.

As you have already found out, removing lines from the middle of a set of lines to be printed just using head and tail in a single pipeline is, at best, tricky. (I didn't get enough sleep last night to say it is impossible, but a way to do it this way is not immediately obvious to me.)
# 5  
Old 08-26-2014
Quote:
Originally Posted by Don Cragun
No sir. We are not here to do your homework for you.
Sir, I am well aware that this forum is not where I will get ready made answers for my assignments. In all humility, I did read the rules before posting here.
Quote:
Originally Posted by Don Cragun
(And those who posted full solutions to your homework problem are subject to infractions for giving inappropriate responses to homework questions.)
I understand that no full solutions are suppose to be posted. But when someone is giving me a full solution to my problem I obviously cannot reject it just because it is against the rules of the forum. I understand that is forum does not condone copying, cheating or other mal practices, but please also understand that I am a Linux noob and is forum is my go to place for clarifying my doubts that I have.

FYI, the solutions posted did work but they weren't exactly what I want. As I said I needed something with the basic filter commands like cut,head,tail,etc.

Quote:
Originally Posted by Don Cragun
The suggestion bakunin made is well worth your attention.
Yes I am looking into it.

Quote:
Originally Posted by Don Cragun
I didn't get enough sleep last night to say it is impossible, but a way to do it this way is not immediately obvious to me.
This is exactly what I am asking. Is it even possible to do what I have asked with basic filtering commands ? If so let me know and give me some basic guidelines(NOT FULL SOLUTIONS) so I can proceed. If not please do tell me that it is not possible then I can try a different approach. Whether it is possible or not is not part of my assignment so I don't think it will be a breach of regulations to answer this yes/no question.

The reason I wanted to know whether my desired operation was possible with head,tail,cut is because that's what we have started studying in my college now.

Final Note:
With all due respect to everyone here, I have followed all the rules and regulations while posting my question and I am not looking to cheat my way through my college assignments nor am I looking for someone who can do my work for me. I am looking for direction so I can get a clear understanding of the UNIX systems.

I know this may sound rude but I am just trying to be as frank and precise as possible. If there are any discrepancies on my part please inform me.
# 6  
Old 08-26-2014
Quote:
Originally Posted by sreyan32
I have followed all the rules and regulations while posting my question and I am not looking to cheat my way through my college assignments nor am I looking for someone who can do my work for me. I am looking for direction so I can get a clear understanding of the UNIX systems.
That is all fine and good. Yes, you did nothing wrong, but others (who posted full solutions) did - still, this is not your fault, but your request in post #6 could be understood as actively asking for such a solution.

But let bygones be bygones and let us start over. Every UNIX-system has an online help for every command. If you do not know about how to use command "foo", then issue the command

Code:
man foo

at the command line to get help about "foo". "man" is short for "manual" and the format of every page is always the same, so you can't start too early to get acquainted with these pages. You can even enter

Code:
man man

to get a a help article about how to use the "man" utility, how the different chapters are organized, etc..

When i told you that "sed" might be worth looking at, we didn't expect you to know what "sed" does. We expected you to research it, first by looking at the man page ("man sed"), then maybe by searching on the internet (enter "man" "sed" or "sed tutorial" in google and you will find lots of hits - others were in your position too).

Unix is a system which - unlike other systems - requires your active work to accomplish anything worthwhile. It is a very well filled toolbox with very specialised, very small precision tools. Put them together in a good way and you can work wonders. Wait for something to happen and you can wait forever. You can use Windows without every getting in touch with filesystems, networks, processes, memory, or anything. In Unix there is no way around that, but once you can do what you want you can do it on virtually any other system too. Instead of knowing where to click you have to understand what you are doing and once you understand what you do you can use that understanding on any other systems. With Windows you may understand what you do, with Unix you have to.

Another thing about Unix is: all the tools are very small - they are doing only one thing, usually, but do this one thing very good. There are no "integrated self-learning 3D-enabled object-oriented web-centric database oriented" whatnots. It is more like an orchestra - with you as the conductor. Violins are sweet-sounding, but make poor basses, while bassons are great basses but lack the lyric quality of the clarinet ...

There is no such thing as a "basic text filter", as you wrote. At least not in the sense of "basic" in contrast to "advanced" - there is only one specialist and another specialist and whilst the specialist excels in its speciality for everything else it is usually a poor tool (if it works at all). You may need the triangle player only one time in a concert, but try to replace him by anything else you will take away from the experience.

So, be a good conductor and learn not only how the tools are used but also how they are intended to be used and what their "area of expertise" is. Your orchestra will sound all the better.

I hope this helps.

bakunin

PS: Once you have a working sed- (or any other) command, post it. We are open to discussion.

PS: another addendum: you might think that using man pages is for beginners. Dead wrong! they are as much a reference as they are a learning tool and all of us Unixers use them on a daily basis. I cannot work a day in office without looking less than ten times at some man page or other.

Last edited by bakunin; 08-26-2014 at 05:03 PM..
This User Gave Thanks to bakunin For This Post:
# 7  
Old 08-26-2014
I agree with everything bakunin said.

In addition, I'll add a few more comments:
  1. I don't understand why you are so interested in printing lines 1 and 3. I don't see anything in the objectives stated in the homework template saying anything about any line number (although it might be appropriate to assume that the header is one line and that that line is the 1st line in the file).
  2. For the stated problem, awk, grep, and sed seem like logical choices to me.
  3. If you want to print lines selected by line numbers, awk and sed would still seem to be logical choices.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Content of attachment is displaying along with subject in mailx

Hi All, I want to send the csv to an email address. I have tried the below two approaches. Approach1: Got error -ksh: uuencode: not found $ uuencode test_file.csv test_file.csv | mailx -s "Attaching test" msdc.kiran@gmail.com </usr/home/test_file.csv -ksh: uuencode: not found Approach2:... (6 Replies)
Discussion started by: ROCK_PLSQL
6 Replies

2. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

3. Shell Programming and Scripting

Displaying Column header in shell script

Hi, I need to display specific columns using select statement and spooled to a file and sending it as e-mail. But i am not seeing column header in my output even i use SET HEADING ON.//PREDEFINED LOGIN DETAILS ${ORACLE_HOME}/bin/sqlplus -s ${DB_LOGIN}/${DB_PASSWD} <<EOF SET FEEDBACK OFF SET... (1 Reply)
Discussion started by: pvelmuru
1 Replies

4. UNIX for Dummies Questions & Answers

Crunch character combination and discard similar content

Hi guys ! I generated the power set of the set S={a,b,c} using crunch: crunch 1 3 abc and get the 39 possible subsets: a b c aa ab ac ba bb bc ca cb cc … (2 Replies)
Discussion started by: beca123456
2 Replies

5. UNIX for Dummies Questions & Answers

Command combination for displaying header and content

Hi everyone, I have UNIX this semester and I am just getting started with the commands. An interesting question came up while discussing the head and tail commands. Suppose that I have text file with the following data in the following format-: NAME ROLL MARKS Sam 05 ... (2 Replies)
Discussion started by: sreyan32
2 Replies

6. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

7. Homework & Coursework Questions

Linux displaying content information

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: Using the fixed length field file called famous.dat make a one-line Unix command - using pipe(s) - to display an... (5 Replies)
Discussion started by: wizardoz123456
5 Replies

8. UNIX for Dummies Questions & Answers

Banner not displaying full content

HI, while using banner command, i am not getting the full content in the screen: banner "UNIX AUTOMATION" # # # # ### # # # # # ####### ####### # # # # ## # # # # # # # # # # # ## ## # # # # # # ... (5 Replies)
Discussion started by: pandeesh
5 Replies

9. Shell Programming and Scripting

Help with rename header content based on reference file problem

I got long list of reference file >data_tmp_number_22 >data_tmp_number_12 >data_tmp_number_20 . . Input file: >sample_data_1 Math, 5, USA, tmp SDFEWRWERWERWRWER FSFDSFSDFSDGSDGSD >sample_data_2 Math, 15, UK, tmp FDSFSDFF >sample_data_3 Math, 50, USA, tmp ARQERREQR . . Desired... (7 Replies)
Discussion started by: perl_beginner
7 Replies

10. Shell Programming and Scripting

Extract specific content from data and rename its header problem asking

Input file 1: >pattern_5 GAATTCGTTCATGTAGGTTGASDASFGDSGRTYRYGHDGSDFGSDGGDSGSDGSDFGSDF ATTTAATTATGATTCATACGTCATATGTTATTATTCAATCGTATAAAATTATGTGACCTT SDFSDGSDFKSDAFLKJASLFJASKLFSJAKJFHASJKFHASJKFHASJKFHSJAKFHAW >pattern_1 AAGTCTTAAGATATCACCGTCGATTAGGTTTATACAGCTTTTGTGTTATTTAAATTTGAC... (10 Replies)
Discussion started by: patrick87
10 Replies
Login or Register to Ask a Question