bash:strings:cut_strings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
bash:strings:cut_strings [2021/01/11 11:48] – peter | bash:strings:cut_strings [2021/01/11 11:49] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BASH - Cut Strings ====== | ||
- | |||
- | To cut portions of a string: | ||
- | |||
- | <code bash> | ||
- | #!/bin/bash | ||
- | Str=" | ||
- | subStr=${Str: | ||
- | echo $subStr | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | This script should print out “My nam” as its output. | ||
- | |||
- | The parameter expansion takes the form ${VAR_NAME: | ||
- | |||
- | Here, S denotes starting position and L indicates the length. | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Extracting Substrings Using Cut ===== | ||
- | |||
- | **cut** can be used to ‘cut’ a portion of a string, aka the substring. | ||
- | |||
- | <code bash> | ||
- | #!/bin/bash | ||
- | Str=" | ||
- | # | ||
- | |||
- | subStr=$(echo $Str| cut -d ' ' -f 1-3) | ||
- | echo $subStr | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | In general to concatenate two variables you can just write them one after another: | ||
- | |||
- | <code bash> | ||
- | a=' | ||
- | b=' | ||
- | c=" | ||
- | echo " | ||
- | > Hello World | ||
- | </ | ||
- | |||
- | or | ||
- | |||
- | <code bash> | ||
- | foo=" | ||
- | foo=" | ||
- | echo " | ||
- | > Hello World | ||
- | </ | ||
- | |||
bash/strings/cut_strings.1610365716.txt.gz · Last modified: 2021/01/11 11:48 by peter