Read Comma Separated Text File in Bash
Fustigate read builtin command
Updated: 12/31/2020 by Computer Hope
On Unix-similar operating systems, read is a builtin control of the Fustigate trounce. It reads a line of text from standard input and splits it into words. These words tin then be used every bit the input for other commands.
Clarification
read reads a single line from standard input, or from the file descriptor fd if the -u pick is used (see -u, below).
Past default, read considers a newline character every bit the end of a line, merely this can exist changed using the -d option.
After reading, the line is split into words according to the value of the special beat variable IFS, the internal field separator. By default, the IFS value is "space, tab, or newline". If read encounters readable characters before encountering an IFS, it considers those characters to exist a word. (For more data most the IFS, see word splitting in bash.)
Tip
To preserve white space at the beginning or the end of a line, it's common to specify IFS= (with no value) immediately before the read command. Afterward reading is completed, the IFS returns to its previous value. For more about this, come across the examples beneath.
read assigns the kickoff word it finds to proper noun, the second word to name2, etc. If there are more words than names, all remaining words are assigned to the final name specified. If only a single name is specified, the entire line is assigned to that variable.
If no name is specified, the input is stored in a variable named REPLY.
Syntax
read [-ers] [-a assortment] [-d delim] [-i text] [-northward nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] [name2 ...]
Options
The read builtin control takes the following options:
-a array | Store the words in an indexed assortment named assortment. Numbering of array elements starts at zero. |
-d delim | Prepare the delimiter character to delim. This character signals the end of the line. If -d is non used, the default line delimiter is a newline. |
-e | Get a line of input from an interactive crush. The user manually inputs characters until the line delimiter is reached. |
-i text | When used in conjunction with -e (and only if -s is not used), text is inserted every bit the initial text of the input line. The user is permitted to edit text on the input line. |
-due north nchars | Finish reading after an integer number nchars characters are read, if the line delimiter has not been reached. |
-N nchars | Ignore the line delimiter. Stop reading only afterward nchars characters are read, EOF is reached, or read times out (see -t). |
-p prompt | Impress the string prompt, without a newline, before get-go to read. |
-r | Use "raw input". Specifically, this option causes read to interpret backslashes literally, rather than interpreting them as escape characters. |
-south | Practise not echo keystrokes when read is taking input from the terminal. |
-t timeout | Fourth dimension out, and render failure, if a consummate line of input is not read within timeout seconds. If the timeout value is goose egg, read volition not read any data, just returns success if input was available to read. If timeout is not specified, the value of the crush variable TMOUT is used instead, if information technology exists. The value of timeout are partial numbers, e.g., 3.5. |
-u fd | Read from the file descriptor fd instead of standard input. The file descriptor should be a small integer. For information near opening a custom file descriptor, see opening file descriptors in bash. |
Go out condition
The exit condition of read is naught unless EOF is encountered, the timeout is exceeded, an error occurs assigning a value to proper name, or the file descriptor provided to -u is invalid.
If a timeout is exceeded, the leave condition is greater than 128.
Examples
while read; do echo "$REPLY"; done
read takes data from the terminal. Type any you'd like, and printing Enter. The text is echoed on the next line. This loop continues until you press Ctrl+D (EOF) on a new line. Because no variable names were specified, the entire line of text is stored in the variable Answer.
while read text; do repeat "$text"; done
Same as above, using the variable name text.
while read -ep "Type something: " -i "My text is " text; do echo "$text"; done
Provides a prompt, and initial text for user input. The user can erase "My text is ", or leave as function of the input. Typing Ctrl+D on a new line terminates the loop.
repeat "Hello, world!" | (read; repeat "$REPLY")
Enclosing the read and echo commands in parentheses executes them in a dedicated subshell. This allows the Reply variable to be accessed past both read and echo. For more information, run across bash command execution environments: subshells.
repeat "one two three four" | while read word1 word2 word3; do echo "word1: $word1" echo "word2: $word2" echo "word3: $word3" washed
Echo "one ii 3 four" and pipe it to the while loop, where read reads the first give-and-take into word1, the second into word2, and everything else into word3. Output:
word1: i word2: two word3: 3 four
echo "one two three four" | while read -a wordarray; practice repeat ${wordarray[one]} done
Same every bit to a higher place, merely assign the words to the elements of an indexed array, wordarray. The numbering starts at zero, then ${wordarray[i]} returns the second word. For more information, see bash arrays. Output:
two
while IFS= read -r -d $'\0' file; do echo "$file" done < <(notice . -print0)
The above commands are the "proper" manner to use observe and read together to procedure files. It's especially useful when you want to practice something to a lot of files that have odd or unusual names. Allow'southward take a look at specific parts of the in a higher place example:
while IFS=
IFS= (with nothing subsequently the equals sign) sets the internal field separator to "no value". Spaces, tabs, and newlines are therefore considered role of a word, which preserves white space in the file names.
Note that IFS= comes after while, ensuring that IFS is altered only within the while loop. For example, information technology won't affect discover.
read -r
Using the -r option is necessary to preserve any backslashes in the file names.
-d $'\0'
The -d pick sets the newline delimiter. Hither, we're setting information technology to the NULL character, ASCII lawmaking goose egg. (An escaped zip enclosed in single quotes, preceded by a dollar sign, is interpreted by bash as Nix. For more than information, see: Expanding strings with interpreted escapes in the fustigate documentation.)
We're using NULL as the line delimiter because Linux file names can comprise newlines, so we need to preserve them. (This sounds atrocious, only yep, it happens.)
Yet, a NULL can never be part of a Linux file proper name, so that's a reliable delimiter to use. Luckily, observe tin use Cypher to delimit its results, rather than a newline, if the -print0 choice is specified:
< <(find . -print0)
Hither, find . -print0 creates a list of every file in and under . (the working directory) and delimit all file names with a NULL. When using -print0, all other arguments and options must precede it, and so brand sure it's the last office of the command.
Enclosing the find control in <( ... ) performs process substitution: the output of the command can be read like a file. In turn, this output is redirected to the while loop using the first "<".
Every iteration of the while loop, read reads one word (a unmarried file name) and puts that value into the variable file, which we specified as the last argument of the read control.
When there are no more file names to read, read returns false, which triggers the finish of the while loop, and the command sequence terminates.
while — Execute a set of deportment while a certain condition is truthful.
find — Notice files within a directory bureaucracy.
Source: https://www.computerhope.com/unix/bash/read.htm
0 Response to "Read Comma Separated Text File in Bash"
Post a Comment