Bash Input/Output: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 52: Line 52:
</syntaxhighlight>
</syntaxhighlight>


=Iterating over Lines from a File in the Same bash Process=
=Iterating over Lines from a File=
==Line Processing Takes Place in a Subprocess==
==Iterating over Lines from a File in the Same bash Process==


==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:
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:
Line 62: Line 64:
</blockquote>
</blockquote>


==With <tt>sed</tt>==
===With <tt>sed</tt>===


This approach has the advantage that lines are read one by one and not buffered:
This approach has the advantage that lines are read one by one and not buffered:
Line 85: Line 87:
</pre>
</pre>


==With <tt>while</tt>==
===With <tt>while</tt>===


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>

Revision as of 04:33, 11 March 2020

Internal

Here-Docs

http://tldp.org/LDP/abs/html/here-docs.html

Echo Multiple Lines to stdout

cat <<EOF
  blah
  ${some_var}
  $(some-command)
  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 > /tmp/test.txt << EOF
blah
EOF

or

(cat << EOF
blah
EOF
) > /tmp/test.txt

To append:

cat >> /tmp/test.txt << EOF
blah
EOF

Iterating over Lines from a File

Line Processing Takes Place in a Subprocess

Iterating over Lines from a File 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:

Iterating over Lines in the Same bash Process with for

With sed

This approach has the advantage that lines are read one by one and not buffered:

filename=...
line_count=$(wc -l ${filename}) || exit 1
line_count=${line_count% *}
line_count=$(echo ${line_count} | sed -e 's/ *//')

line_number=1

while [ ${line_number} -le ${line_count} ]; do

    local line
    line=$(sed -n ${line_number}p ${filename})

    # process line ...

    ((line_number++))
done

With while

while IFS= read -r line; do
    echo ${line}
   ...
done <<< "$(cat ./myfile.txt)"

Read First Line from a File

head -i <file>

Read the First Line of a Command's Output

bash --version | head -n 1

Extracting a Line Specified by Its Number from a File

With head and tail

Extract line 15 (line numbers are 1-based) with head and tail:

cat ./test.txt | head -15 | tail -1

With sed

Print a Line Specified by Its Number with sed

echo Formatting

Bold:

echo -e "\033[1msomething bold \033[0m"

Red:

echo -e "\033[31msomething red\033[0m"

Quoted String Expansion

$'\012' # Octal value
$'\x0a' # Hexadecimal value

More details:

Quoted String Expansion

Reading from stdin

Interactively Read Input from stdin