mapfile - Linux
Overview
mapfile
, also known as readarray
, is a bash built-in command used to read lines from the standard input into an array variable. This command is useful for scripts that need to process lists or outputs from other commands line-by-line efficiently. It is particularly valuable in scenarios where handling large amounts of data or complex string manipulation tasks are necessary.
Syntax
The basic syntax of the mapfile
command is as follows:
mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
- array: The name of the array to create or modify. If omitted, the default array name is
MAPFILE
.
Options/Flags
- -d delim: Specify a delimiter character. The default delimiter is a newline.
- -n count: Limit the number of lines read to
count
. By default,mapfile
reads all lines until EOF. - -O origin: Begin storing lines in the array at the index
origin
. The default index is 0. - -s count: Discard the first
count
lines read. - -t: Remove a trailing newline from each line read.
- -u fd: Read input from file descriptor
fd
. - -C callback: Execute
callback
everyquantum
lines. Thecallback
is evaluated after the line is read and before the array element is assigned. - -c quantum: The number of lines read between each invocation of the
callback
.
Examples
Basic Usage:
mapfile my_array < myfile.txt
This reads lines from myfile.txt
into the array named my_array
.
Using a Custom Delimiter:
mapfile -d ':' my_array < /etc/passwd
Here, mapfile
reads the /etc/passwd
file using ‘:’ as the delimiter, useful for parsing colon-separated files.
Reading a Specific Number of Lines and Skipping Lines:
mapfile -n 10 -s 2 my_array < myfile.txt
This command skips the first 2 lines and reads the next 10 lines from myfile.txt
into my_array
.
Common Issues
- Memory Limitations: Large files can cause performance issues. In such cases, consider reading the file in chunks.
- Handling Special Characters: Ensure the
-d
option’s delimiter does not occur in the file unexpectedly, as it can distort the array’s formation.
Integration
mapfile
can be integrated with other commands like find
or grep
in scripts to handle complex data processing tasks:
find /var/log -name "*.log" | mapfile -t log_files
cat "${log_files[@]}" | grep "ERROR" > error_logs.txt
This script finds all log files, reads them into an array, then concatenates them and filters lines containing “ERROR”.
Related Commands
- read: Reads a single line of input.
- declare: Used to declare array variables which can be used with
mapfile
.
Further information can be found in the Bash official documentation or man pages, accessible via man bash
on your Linux terminal.