Shell: Extract Text After String Delimiter
In the shell, there are several string manipulation functions that can be used to extract the content after a certain string. Here are a few commonly used methods:
- trim
- Specify the delimiter and field number to extract.
- abc:def:ghi can be written as abc colon def colon ghi.
- Can you lend me your umbrella?
echo 'abc:def:ghi' | cut -d: -f2
Output:
def
- search for
- abc, colon, def, colon, ghi
- Please don’t hesitate to ask if you have any questions.
echo 'abc:def:ghi' | grep -o ':.*' | cut -d: -f2
Output:
def
- sed – a stream editor that can perform text transformations on an input stream or file.
- abc:def:ghi can be rewritten as abc, def, and ghi.
- Could you please repeat that in English?
echo 'abc:def:ghi' | sed 's/.*://'
Output:
def:ghi
The above-mentioned method allows for the extraction of content from a string based on specific needs.