Executing many commands at once

Hi,

I want to run these two commands one after the other.

awk 'BEGIN {OFS="\t"} {print $2}'
sort -u 

rather than typing awk 'BEGIN {OFS="\t"} {print $2}' file1 > file2, then sort -u file2 > file3. Is it possible to run both commands on file1 then get output file3?

Its kinda hard for me to explain but i hope its clear

Kyle

Does this work for you?

awk 'BEGIN {OFS="\t"} {print $2}' file1 | sort -u > file3

hey thanks

what if I wanted to do like 10 commands at once?? should i just have | in between each command?

Kyle

so lets say I wanted to do 3 commands at once... this would work?

awk 'BEGIN {OFS="\t"} {print $1, $2}' file.txt | awk 'BEGIN {OFS="\t"} {print $1}' | sort -u > file2.txt

The best way to learn it is to try it out! :slight_smile:

The pipe "|" only works when one command sends it output to standard out and the next command in the pipeline accepts input to standard in.

You might benefit from a review of this Wikipedia page:

Pipeline (Unix) - Wikipedia, the free encyclopedia

If the commands aren't operating on output of previous commans, you can just put them all in a file and (after giving it execute perms - often "chmod 755 file") run that file instead:

$ cat > mycommands
command1
command2
command3
... 
command259
(CTRL-D)
$ chmod 755 mycommands
$ ./mycommands

If the next command shouldn't be run if the previous command fails, use:

$ cd /foo && cat abc
# only does the "cat" if you can chdir to /foo

or, if you should only run it on failure:

$ cd /foo || echo "Can't find /foo!"