This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
os_cp:redirects_pipes [2023/03/31 15:22] jkonczak [cut] |
os_cp:redirects_pipes [2025/04/07 21:07] (current) jkonczak added examples |
||
---|---|---|---|
Line 14: | Line 14: | ||
<small> | <small> | ||
+ | ''**join** [-t //separator//] [-1 //fieldNumberInFile1//] [-2 //fieldNumberInFile2//] //file1// //file2//'' | ||
+ | parses two sorted files and prints their lines joined on specified fields. | ||
+ | |||
''**fold** [-w //width//] [//file//]...'' outputs input files (or standard input) | ''**fold** [-w //width//] [//file//]...'' outputs input files (or standard input) | ||
forcing a line break whenever a line would exceed //width// (that defaults to 80). | forcing a line break whenever a line would exceed //width// (that defaults to 80). | ||
Line 170: | Line 173: | ||
~~Exercise.#~~ Redirect standard output and the standard error of the ''find /var/spool/'' command to the same file. | ~~Exercise.#~~ Redirect standard output and the standard error of the ''find /var/spool/'' command to the same file. | ||
+ | |||
+ | ++++ Examples | {{page>so:redirects:out-en&inline}} ++++ | ||
=== Input redirections === | === Input redirections === | ||
Line 198: | Line 203: | ||
~~Exercise.#~~ Use ''bc'' to calculate ''sqrt(2.0000)'' in non-interactive mode and redirect its output to a file. | ~~Exercise.#~~ Use ''bc'' to calculate ''sqrt(2.0000)'' in non-interactive mode and redirect its output to a file. | ||
+ | |||
+ | ++++ Examples | {{page>so:redirects:in&inline}} ++++ | ||
<small> | <small> | ||
Line 220: | Line 227: | ||
| ''>>'' | opens //word// for appending and replaces //file_number// with the file | | | ''>>'' | opens //word// for appending and replaces //file_number// with the file | | ||
| ''<>'' | opens //word// for reading and writing and replaces //file_number// with the file | | | ''<>'' | opens //word// for reading and writing and replaces //file_number// with the file | | ||
- | | ''<<'' | 1) creates a temporary file \\ 2) reads an input line \\ 3) if the line is //word//, go to step 6 \\ 4) if there are no quotes (a pair of ''"'' or <html><code>'</code></html>) in //word//, performs the expansion((E.g., ''$VAR'' is substituted with its value, ''`date`'' is replaced by output of the date command etc.)) on the line \\ 5) writes the line to the temporary file \\ 6) opens the temporary file for reading \\ 7) replace //file_number// with the file \\ The command is run once this is done | | + | | ''<<'' | 1) creates a temporary file \\ 2) reads an input line \\ 3) if the line is //word//, goes to step 7 \\ 4) if there are no quotes (a pair of ''"'' or <html><code>'</code></html>) enclosing the //word//, performs the expansion((E.g., ''$VAR'' is substituted with its value, ''`date`'' is replaced by output of the date command etc.)) on the line \\ 5) appends the line to the temporary file \\ 6) goes to step 2 \\ 7) opens the temporary file for reading \\ 8) replace //file_number// with the file \\ The command is run once this is done | |
| ''<<-''| same as ''<<'', but after step 2 adds a step: \\ 2a) erase all leading tab characters (''\t'') \\ warning: spaces are not erased | | | ''<<-''| same as ''<<'', but after step 2 adds a step: \\ 2a) erase all leading tab characters (''\t'') \\ warning: spaces are not erased | | ||
| ''<<<''| warning: this is a Bash extension \\ 1) creates a temporary file \\ 2) writes //word// to it \\ 3) writes a newline to it \\ 4) opens the temporary file for reading \\ 5) replace //file_number// with the file \\ The command is run once this is done | | | ''<<<''| warning: this is a Bash extension \\ 1) creates a temporary file \\ 2) writes //word// to it \\ 3) writes a newline to it \\ 4) opens the temporary file for reading \\ 5) replace //file_number// with the file \\ The command is run once this is done | | ||
- | | ''<&'' | if //word// is a number: duplicates a readable descriptor number //word// to the number //file_number// \\ if //word/// is ''-'': closes a descriptor number //file_number// | | + | | ''<&'' | if //word// is a number: duplicates a readable descriptor number //word// to the number //file_number// \\ if //word// is ''-'': closes a descriptor number //file_number// | |
- | | ''>&'' | if //word// is a number: duplicates a writeable descriptor number //word// to the number //file_number// \\ if //word/// is ''-'': closes a descriptor number //file_number// | | + | | ''>&'' | if //word// is a number: duplicates a writeable descriptor number //word// to the number //file_number// \\ if //word// is ''-'': closes a descriptor number //file_number// | |
| ''&>'' | warning: this is a Bash extension \\ warning: this does not allow providing //file_number// (in fact, ''&'' is the file number) \\ opens //word// for writing, truncates it and replaces streams 1 and 2 with the file | | | ''&>'' | warning: this is a Bash extension \\ warning: this does not allow providing //file_number// (in fact, ''&'' is the file number) \\ opens //word// for writing, truncates it and replaces streams 1 and 2 with the file | | ||
| ''&>>''| warning: this is a Bash extension \\ warning: this does not allow providing //file_number// (in fact, ''&'' is the file number) \\ opens //word// for appending and replaces streams 1 and 2 with the file | | | ''&>>''| warning: this is a Bash extension \\ warning: this does not allow providing //file_number// (in fact, ''&'' is the file number) \\ opens //word// for appending and replaces streams 1 and 2 with the file | | ||
Line 330: | Line 337: | ||
~~Exercise.#~~ Create a named pipe //p//. Redirect input of ''fold'' from //p// | ~~Exercise.#~~ Create a named pipe //p//. Redirect input of ''fold'' from //p// | ||
in one terminal, and redirect output of ''ps -eF'' to //p// in another terminal. | in one terminal, and redirect output of ''ps -eF'' to //p// in another terminal. | ||
- | \\ Then repeat the commands, running the''ps'' before the ''fold''. | + | \\ Then repeat the commands, running the ''ps'' before the ''fold''. |
</small> | </small> | ||
+ | |||
+ | ++++ Examples | {{page>so:redirects:pipes1-en&inline}} ++++ | ||
===== Filters ===== | ===== Filters ===== | ||
Line 360: | Line 369: | ||
~~Exercise.#~~ Run ''seq 25 > //file//''. Then run ''tail -f //file//'' in one | ~~Exercise.#~~ Run ''seq 25 > //file//''. Then run ''tail -f //file//'' in one | ||
terminal and append (with output redirection) some data to //file//. | terminal and append (with output redirection) some data to //file//. | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:head_tail&inline}} ++++ | ||
==== grep ==== | ==== grep ==== | ||
Line 406: | Line 417: | ||
\\ | \\ | ||
List all files containing ''ecdsa'' in ''~/.ssh''. | List all files containing ''ecdsa'' in ''~/.ssh''. | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:grep&inline}} ++++ | ||
==== cut ==== | ==== cut ==== | ||
Line 427: | Line 440: | ||
''egrep '^[Ee]{2}' /usr/share/myspell/en_US.dic'' a slash and all that follows | ''egrep '^[Ee]{2}' /usr/share/myspell/en_US.dic'' a slash and all that follows | ||
it. | it. | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:cut-en&inline}} ++++ | ||
==== sort ==== | ==== sort ==== | ||
Line 471: | Line 486: | ||
~~Exercise.#~~ Sort the file by the second column (alphabetically) and by the third column (numerically). | ~~Exercise.#~~ Sort the file by the second column (alphabetically) and by the third column (numerically). | ||
</small> | </small> | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:sort&inline}} ++++ | ||
==== wc, uniq, nl ==== | ==== wc, uniq, nl ==== | ||
Line 503: | Line 520: | ||
\\ | \\ | ||
Then pipe it through ''sort'' and ''uniq'' so that you see how many times each result was hit. | Then pipe it through ''sort'' and ''uniq'' so that you see how many times each result was hit. | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:wc_uniq&inline}} ++++ | ||
<small> | <small> | ||
Line 537: | Line 556: | ||
* to squeeze all spaces | * to squeeze all spaces | ||
* remove all the letters ''rwx'' | * remove all the letters ''rwx'' | ||
+ | |||
+ | ++++ Examples | {{page>so:pipes_filters:tr-en&inline}} ++++ | ||
<small> | <small> |