Bash Input/Output: Difference between revisions
Jump to navigation
Jump to search
Line 34: | Line 34: | ||
==With <tt>for</tt>== | ==With <tt>for</tt>== | ||
Note that the following approach works better for small files. If the file is large, the content will be first cached in memory, and it may look like the command is irresponsive: | |||
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | <blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | ||
:[[Bash_for#Iterating_over_Lines_in_the_Same_bash_Process|Iterating over Lines in the Same bash Process]] | :[[Bash_for#Iterating_over_Lines_in_the_Same_bash_Process|Iterating over Lines in the Same bash Process with <tt>for</tt>]] | ||
</blockquote> | </blockquote> | ||
==With <tt>sed</tt>== | |||
=Extracting a Line Specified by Its Number from a File= | =Extracting a Line Specified by Its Number from a File= |
Revision as of 21:10, 18 April 2017
Internal
Echo Multiple Lines to stdout
cat <<EOF blah ${some_var} blah EOF
- It will perform variable substitution. If you don't want that, escape \${some_var}.
- `...` sequences should be escaped as shown here: \`...\` otherwise they will executed before the output is sent to stdout.
- \ will join lines. If you want "\" in the output, then you should escape it:
... blah blah \\ ...
Echo Multiple Lines into a File
(cat << EOF blah EOF ) > /tmp/test.txt
Iterating over Lines in the Same bash Process
With for
Note that the following approach works better for small files. If the file is large, the content will be first cached in memory, and it may look like the command is irresponsive:
With sed
Extracting a Line Specified by Its Number from a File
With head and tail
Extract line 15 (line numbers are 1-based):
cat ./test.txt | head -15 | tail -1