Closed Captioning Closed captioning available on our YouTube channel

How to extract substrings: 2-Minute Linux Tips

Network World | May 27, 2022

In this Linux tip, learn several ways to extract substrings on Linux.

Copyright © 2022 IDG Communications, Inc.

Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at several ways to extract substrings on Linux.
Using bash parameter expansion, we could do something like this:
Notice that the 16 represent the position of the first character you want to pull from the string and that, in this method of grabbing a substring, what we would consider the first character is treated as position zero.
That's easier to see in this example:
This selects the substring starting with the 7th character and ending one character before the last.
Using the cut command, the first character is position 1. You can specify a range using a command like this:
The cut command can also work with delimiters. If you have a file contain colon-separated fields – like the /etc/passwd file, you can extract a particular field and the process it. In this command, we pull the 7th field and count how many of the accounts use each of the shells:
The awk command is another command that allows easy extraction of substrings. This command will print the 1st and 3rd fields from a comma-separated file of addresses.
The expr command works like this:
That’s your Linux tip for extracting substrings with Linux commands.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and
Featured videos from