Task: extract a substring with delimiters from an existing string, specifying a certain number. That is, for example, from the string "ABC,DEF,GHI" extract the string "DEF", specifying that it is the 2nd substring and the delimiter is a comma.
For this we will use the cut utility.
a="String1,String2,String3"
echo "${a}" | cut -d ',' -f 2
here:
- -d : specifies the delimiter character
- -f : specifies the substring number (starting from 1)
If you need to extract a substring with a TAB delimiter - then you simply do not need to specify the -d parameter, since TAB is the delimiter by default.
Here is another example:
a="This is just a test"
b=`echo "${a}" | cut -d ' ' -f 3`
The output will be "just".
Comments