User Tools

Site Tools


bash:files:read_a_file:read_fields_from_a_file

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash:files:read_a_file:read_fields_from_a_file [2021/01/26 13:27] – [Fields are NOT separated with white-space] peterbash: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 34: Line 34:
  
 ---- ----
 +
 +===== Tab-delimited files =====
  
 <WRAP info> <WRAP info>
-For tab-delimited files, use **IFS=$'\t'** though beware that multiple tab characters in the input will be considered as one delimiter (and the **IFS=$'\t\t'** workaround will not work in Bash).+**NOTE:**  For tab-delimited files, use **IFS=$'\t'**
 + 
 +<WRAP important> 
 +**WARNING:**  Multiple tab characters in the input will be considered as one delimiter (and the **IFS=$'\t\t'** workaround does not work in Bash). 
 + 
 +</WRAP> 
 + 
 +</WRAP> 
 + 
 +---- 
 + 
 +===== 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 46: Line 59:
  
 <code bash> <code bash>
-read -r first last junk <<< 'Bob Smith 123 Main Street Elk Grove Iowa 123-555-6789'+read -r first last junk <<< 'Bob Smith 123 Main Street Saint Helier Jersey'
 </code> </code>
 +
 +<WRAP info>
 +**NOTE:**
  
   * **first**:   will contain "Bob"   * **first**:   will contain "Bob"
Line 53: Line 69:
   * **junk**:  holds everything else.   * **junk**:  holds everything else.
  
-The throwaway variable **_** can be used as a "junk variable" to ignore fields.+</WRAP> 
 + 
 +----
  
-  * 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 variable =====
  
 <code bash> <code bash>
Line 61: Line 79:
 </code> </code>
  
-  We skip the first two fields, then read the next three. + 
-  * The final **_** can absorb any number of fields.+<WRAP info> 
 +**NOTE:**  The throwaway variable **_** can be used as a "junk variable" to ignore fields
 + 
 +  * Itand any other variable, can be used more than once in a single read command, if we don't care what goes into it. 
 + 
 +  * The first two fields are skipped. 
 +  * The next three fields are reading into variables
 +  * The final **_** will absorb any remaining fields on the line.
     * 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:**  This usage of **_** is only guaranteed to work in Bash.
  
-  * 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 **_** is a common Bash convention. 
 + 
 +</WRAP>
  
 </WRAP> </WRAP>
  
 +----
  
  
bash/files/read_a_file/read_fields_from_a_file.1611667629.txt.gz · Last modified: 2021/01/26 13:27 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki