Piping commands


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Piping commands
# 8  
Old 03-28-2013
Quote:
Originally Posted by iliya24
Hi
I am tryin to undertand piping

command1|command2

from what i learn output of cammand 2 is an intput for command 1 right?

If so .
What dose next sequence do


cat f1 >> f2 | grep '^[A-Da-F]'

I think it takes context of f1 and Concatenate's it to f2 and then looks for ....i don't know..
Thx.
I'm extremely confused by some of the comments I've seen in this thread.

First:
Code:
command1|command2

sets things up so the standard output of command 1 becomes the standard input for command2. This is exactly backwards from what you said above.

Then the question: What does the command:
Code:
cat f1 >> f2 | grep '^[A-Da-F]'

do?

First, the shell creates a pipe taking the output of cat f1 >> f2 and feeding it into the input of grep '^[A-Da-F]'. Then the shell sets up the two commands (cat and grep).

For the cat command, the shell sets the output from cat f1 to be appended to the end of the current contents of the file named f2. This causes the contents of the file f1 to be added to the end of the contents of the file f2. This output redirection causes the output side of the pipe from cat to grep set up above to be closed.

Assuming you're in the C or POSIX Locale, the grep command treats the argument '^[A-Da-F]' either as a request to have grep print any input line it reads if the first character on that line (indicated by the circumflex (^) at the start of the expression) is a collating symbol between A and D, inclusive, (i.e., A, B, C, or D) or to print a diagnostic message noting that the range expression a-F is invalid because F comes before a in the C and POSIX locales' collating sequences. In other locales the results are unspecified, but on many systems (if you don't get an invalid range expression error), you might also print lines where the first character on the line is one of the above with various diacritical markings (such as À, Á, or Ḉ).

Since the output from cat was redirected to f2 after the pipe was set up, the write end of the pipe will be closed and grep will hit end-of-file the first time it tries to read from the pipe. So, the grep command will never produce any output (other than the possible diagnostic message complaining about an invalid range expression).

I hope this helps.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Piping commands using xargs

Need help in piping commands using xargs I have several .tar.gz files that I need to list the folder content in a subdirectory. For example, a.tar.gz b.tar.gz c.tar.gz The following command works great for each .tar.gz file but it's a pain to run the tar command for each file. tar -tf... (11 Replies)
Discussion started by: april
11 Replies

2. Shell Programming and Scripting

Proper chaining and piping of commands

can someone please help me fix the below one liner? what i want to do is run a command, and if that command is successful, pipe the output of the command to another command. i'd prefer not to use any temp files for this. who -blahblah ; if ; then echo exit; fi | egrep username (2 Replies)
Discussion started by: SkySmart
2 Replies

3. UNIX for Dummies Questions & Answers

Piping multiple commands

Using the below code I want to find all .sff files and extract them. This works but it seems very cheap. Is there a safer more efficient way to go about this? #!/bin/bash G1=(/home/dirone) find ${G1} -type f -name \*.sff | xargs python /usr/local/bin/sff_extract.py (3 Replies)
Discussion started by: jrymer
3 Replies

4. Shell Programming and Scripting

Piping through commands read as variables from file

This is going to be part of a longer script with more features, but I have boiled it down to the one thing that is presently stumping me. The goal is a script which checks for updates to web pages that can be run as a cron job. The script reads (from a tab-delim file) a URL, an MD5 digest, and an... (1 Reply)
Discussion started by: fitzwilliam
1 Replies

5. UNIX for Dummies Questions & Answers

Piping GREP

Hi, I need to use a double grep so to speak. I need to grep for a particular item say BOB and then for each successful result I need to grep for another item say SMITH. I tried grep "BOB" filename | grep "SMITH" but it does not seem to work. I can achieve my desired result using an... (12 Replies)
Discussion started by: mojoman
12 Replies

6. Shell Programming and Scripting

Piping in Perl

Hi All, I am trying to perform the below csh code in Perl, but i am unfamiliar with Perl. Can anybody give me some advice on it ? Csh Code: cat filename |grep AAA| grep BBB| awk '{print("already_appended")' (11 Replies)
Discussion started by: Raynon
11 Replies

7. Shell Programming and Scripting

Piping to ex from a script

Is anyone piping commands to ex from scripts? I.E. echo '%s/change this/to that/\nwq' | ex file.name I've been using it for years with AIX, Solaris, SGI, with variations ksh and Mandriva and others with pdksh. I've just started using CentOS with ksh and it no longer works. I've tried single... (2 Replies)
Discussion started by: mph
2 Replies

8. Shell Programming and Scripting

Piping / Executing

I've got a file with lots of commands I want to run in it. They're formatted like so: cp /path/to/file /path/to/new/file and on and on and on. Hundreds of them. Anyways, I'd like to execute them one at a time, then check what time it is, and repeat this process until 7am. I can... (3 Replies)
Discussion started by: ProFiction
3 Replies

9. Programming

Help with piping program

Hi, I am trying to write a program that will pipe any number of programs together like in the linux shell. As an example, the below code tries to execute "cat data | grep int | cut -b 1-10." The problem is that the programs never get executed for some reason. It seems like the first program... (3 Replies)
Discussion started by: PuppyHusher
3 Replies

10. Shell Programming and Scripting

piping

I am using pipes (specifically piping out) in Perl to put an array from one file into an array in a different file. I can't figure out how to transfer the array. I kow how to open the pipe : open (FILEHANDLE, "| file") or die~ but how do I transfer the array. I think it has something to do with... (1 Reply)
Discussion started by: lnatz
1 Replies
Login or Register to Ask a Question