- The best Mini ITX PC cases of 2025: Expert recommended
- From Copilot to agent - AI is growing up, and CISOs need to be ready
- My favorite Apple Watch for tracking my workouts is 32% off at major retailers
- Discover the Cisco Catalyst Center Fundamentals (CCFND) Training Program
- This robot vacuum's dustbin doubles as a handheld vacuum (and it's on sale)
How to work with substrings on Linux

$ echo "Focus on Peace on Earth" | cut -d' ' -f3,5 Peace Earth $ echo "one two three 4 5 6" | cut -d' ' -f1-3,6 one two three 6
To use an alternate delimiter (in this case, a colon), use a command like this:
$ cut -d':' -f1-3,5,6 /etc/passwd | tail -n 5 justme:x:1004:JustMe:/home/justme lola:x:1006::/home/lola dumdum:x:1007::/home/dumdum
With awk, you can use more than one delimiter. In the following example, two delimiters are specified, so the awk command accepts either a colon or a blank to separate fields. The first two lines display the file, and the last two lines show the command and result.
$ cat file Monday:1 Tuesday:2 Wednesday:3 Thursday:4 Friday:5 $ awk -F'[: ]' '{OFS=" ";print $1,$3,$4}' file Monday Tuesday 2
Selecting substrings
To select an arbitrary sequence or characters from a string, you can use an awk command like the one below in which the $0 represents the entire phrase, 10 represents the first character position to be grabbed and 5 is the length of the string to be displayed.
$ echo "Focus on Peace" | awk '{print substr($0,10,5)}' Peace
To do the same kind of thing with the cut command, you would use a command like this in which the 13th through 22nd characters are extracted from the phrase and displayed.
$ echo "Linux is an impressive OS" | cut -c 13-22 impressive
In this next command, the cut command displays the 7th-12th characters from the lines in a file. The head command simply limits the display to the first 4 lines of output.
$ cut -c 7-12 sayings | head -4 with 3 and ov nd be and be
Using grep
You can use the grep command to select multiple words from a file. In this example, only the selected words are displayed, not the entire lines. This is because the -o (display only the matched items) option is being used.