LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2009, 07:35 PM   #1
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Rep: Reputation: 15
Pipe command


Okay, I'm having a hard time. I'm currently going to college, and one of the classes for some odd reason I have to take which is Linux. I'm going to college for web design, and Linux was one of the classes I had to complete (why I don't know). But honestly, I've been working on this question all week and have unable to answer correctly, each time I send it in the teacher says it is incorrect. I'm taking online classes at Baker College (just so you know). Here's the question...

Let us say there is a command "double" which displays each input line twice in the output and let us assume that the input file input.txt has 5 lines of text. How many lines will be output by the following command?

cat input.txt | double | double | double

My first answer was 35, which was wrong, then I got 15 and that is also wrong. I'm not exactly sure what I am doing wrong? I know that the pipe command redirects the output of one command to the input of another command. Which is like this: first_command | second_command and so forth, and can do multiple commands.

I do not know what I am doing wrong, and the teacher isn't helping me out at all. I have read and reread the book of ours, searched online to get an idea and that hasn't helped so far. Any thoughts on this would be highly appreciated. If you do not want to answer that is fine, but I am not sure what I am doing wrong at all.

Thanks, much appreciated!!
 
Old 03-11-2009, 07:54 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Mate ... that's not even a "linux question" but a simple Maths problem, and even w/o
a shell around, or a program that does what you're asking (double) you should see that
within a few seconds ... the result (even if only *one* of the processes along the
chain of pipes does a "times two" somewhere) the result will be even ...

5 * 2 * 2 * 2


*sigh*




Cheers,
Tink
 
Old 03-11-2009, 08:00 PM   #3
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Omg, I feel stupid!!! I swear, being sick and working with Linux isn't easy. I've been sick all week and just came back a while ago from the doctors.

So that would be 30 or 40, gosh I hate math. lol

Do I want to go 5*2 5*2 5*2 or 5*2*2*2?

Like I said, I hate math... lol


----

Nevermind, I figured it out... AGAIN thank you!!!

Last edited by shorte85; 03-11-2009 at 08:01 PM.
 
Old 03-12-2009, 12:38 AM   #4
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Rep: Reputation: 33
Hi

I will try to explain pipe clearly. The use of pipe is to redirect the output which means, the output of the first command will be given as input to the second command.
Eg:

$ ls | less

ls - list all files in that directory

less - to display one page at a time

Here the output of the first command is to list all the files in that directory and this output will be displayed one page at a time by using less.


Try it out practically. Then its easy otherwise its very hard. I too am a beginner but i am trying to learn.

ALL THE BEST.


PHP Code:
1of intelligence and 99of hard work makes you great person suman 
 
Old 03-12-2009, 03:08 AM   #5
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
If you want to play with it some more, entering this into a shell will cause 'double' to behave as you described:

Code:
$ double () {
gawk '{print $0; print $0}'
}

$ echo hi there | double
hi there
hi there
 
Old 03-12-2009, 11:21 PM   #6
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks guys!! I appreciate it. I think I understand it more clearly now. I appreciate it!!

It's kind of like doing the following:

$ ls -l | more

right?
 
Old 03-13-2009, 12:27 AM   #7
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Er, well kind of, in that that command also uses a pipe. These articles may make things clearer:
http://www.linux-mag.com/id/4038/
http://www.linux-mag.com/id/4705
http://www.linux-mag.com/id/5103
 
Old 03-13-2009, 07:40 AM   #8
shaolin77
Member
 
Registered: Aug 2004
Distribution: Fedora and Ubuntu
Posts: 44

Rep: Reputation: 15
Quote:
Originally Posted by shorte85 View Post
Okay, I'm having a hard time. I'm currently going to college, and one of the classes for some odd reason I have to take which is Linux. I'm going to college for web design, and Linux was one of the classes I had to complete (why I don't know). But honestly, I've been working on this question all week and have unable to answer correctly, each time I send it in the teacher says it is incorrect. I'm taking online classes at Baker College (just so you know). Here's the question...

Let us say there is a command "double" which displays each input line twice in the output and let us assume that the input file input.txt has 5 lines of text. How many lines will be output by the following command?

cat input.txt | double | double | double

My first answer was 35, which was wrong, then I got 15 and that is also wrong. I'm not exactly sure what I am doing wrong? I know that the pipe command redirects the output of one command to the input of another command. Which is like this: first_command | second_command and so forth, and can do multiple commands.

I do not know what I am doing wrong, and the teacher isn't helping me out at all. I have read and reread the book of ours, searched online to get an idea and that hasn't helped so far. Any thoughts on this would be highly appreciated. If you do not want to answer that is fine, but I am not sure what I am doing wrong at all.

Thanks, much appreciated!!

Seeing how there have been good explanations posted for the pipe command...I'll add on why your college is having you take a Linux course....I'll keep it short and sweet when it comes to the Internet and Web design...Linux is not the new OS in town....the percentage of web servers running Linux compared to running windows is very high.

Apache...runs on Linux which is used host websites...websites are designed with MySQL back end databases...or with php which is just like asp.net...so learning Linux would be beneficial to you...and give you a better understanding of web design outside of a windows environment.

Its nice to know that colleges are providing Linux Classes...when I went to school...Linux classes weren't offered as part of web design..so your getting a bonus... don't take it for granted!

Last edited by shaolin77; 03-13-2009 at 07:41 AM.
 
Old 03-13-2009, 02:18 PM   #9
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
I guess I can see what you are saying shaolin77, thanks for explaining that to me! My other classmates were also wondering the samething. I'll definitely have to let them know what you said.
 
Old 03-13-2009, 06:08 PM   #10
rysiekmus2
Member
 
Registered: Mar 2007
Location: Manchester UK
Distribution: Sabayon
Posts: 30

Rep: Reputation: 17
No offence, but if someone wants to go to college and have anything to do with computers, whatever the technology, Math is and always will be always there. Math and computers always go together.
You must have picked the right college, so be grateful for the teachers, I am sure they know what they are doing.
Just because you are studying web designing does not mean you don't need to know what technology you will see in the real world. That's what's schools are for, explore everything and get prepared for the tough world, so don't say you don't like Math.

Good luck in the real world.
Richard.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a pipe in a cron command? jlinkels Linux - Software 3 01-30-2009 01:57 PM
kill command in a pipe bujecas Linux - General 1 02-02-2006 07:55 AM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
how to: a pipe command? youngstorm Linux - Newbie 2 02-02-2005 10:52 AM
Pipe command question satimis Linux - Newbie 8 01-07-2004 10:54 AM


All times are GMT -5. The time now is 02:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration