bash:files:read_a_file:read_fields_from_a_file
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
bash:files:read_a_file:read_fields_from_a_file [2021/01/26 13:25] – peter | bash:files:read_a_file:read_fields_from_a_file [2021/01/26 13:44] (current) – [Field splitting, white-space trimming, and other input processing] peter | ||
---|---|---|---|
Line 30: | Line 30: | ||
<WRAP info> | <WRAP info> | ||
- | **NOTE: | + | **NOTE: |
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Tab-delimited files ===== | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | <WRAP important> | ||
+ | **WARNING: | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
- | For tab-delimited files, use **IFS=$' | + | ===== Not knowing how many fields a line contains ===== |
You do not necessarily need to know how many fields each line of input contains. | You do not necessarily need to know how many fields each line of input contains. | ||
Line 42: | Line 59: | ||
<code bash> | <code bash> | ||
- | read -r first last junk <<< | + | read -r first last junk <<< |
</ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE:** | ||
* **first**: | * **first**: | ||
Line 49: | Line 69: | ||
* **junk**: | * **junk**: | ||
- | The throwaway variable **_** can be used as a "junk variable" | + | </ |
+ | |||
+ | ---- | ||
- | * It (or indeed any variable) can also be used more than once in a single read command, if we don't care what goes into it: | + | ===== Throwaway |
<code bash> | <code bash> | ||
Line 57: | Line 79: | ||
</ | </ | ||
- | | + | |
- | * The final **_** can absorb any number of fields. | + | <WRAP info> |
+ | **NOTE: | ||
+ | |||
+ | * It, and any other variable, can be used more than once in a single | ||
+ | |||
+ | * The first two fields are skipped. | ||
+ | * The next three fields are reading into variables. | ||
+ | * The final **_** will absorb any remaining | ||
* It does not need to be repeated there. | * It does not need to be repeated there. | ||
- | This usage of **_** is only guaranteed to work in Bash. | + | <WRAP important> |
+ | **WARNING: | ||
- | * Many other shells use _ for other purposes that will at best cause this to not have the desired effect, and can break the script entirely. | + | * Many other shells use **_** for other purposes that will at best cause this to not have the desired effect, and can break the script entirely. |
- | * It is better to choose a unique variable that isn't used elsewhere in the script, even though _ is a common Bash convention. | + | * It is better to choose a unique variable that isn't used elsewhere in the script, even though |
+ | |||
+ | </ | ||
</ | </ | ||
+ | ---- | ||
bash/files/read_a_file/read_fields_from_a_file.1611667540.txt.gz · Last modified: 2021/01/26 13:25 by peter