How to extract substring in Bash Shell

In this tutorial, How to extract substring in Bash Shell on Linux. You can use select substring from the string. The awk command has a few useful functions. It is a substr function.

Function substr mean Returns a substring.

The syntax

substr(S,P,N)
  • P: starting at position
  • N: it is returns N number of character’s from string S.

For example, I will use a string “HuuPV2123456789” with length is 15 and starting positions is 10.

$ echo "HuuPV2123456789" | awk '{ printf "%s", substr($0, 10, (length($0)-9)) }'

The result as below

456789

man page funtion substr here

, , , ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.