How to Extract a Percentage Number from a String in Shell Scripts
Автор: vlogize
Загружено: 2025-09-30
Просмотров: 0
Описание:
Discover how to extract a percentage value from a string using `sed` or `awk` in shell scripting. Learn with clear examples and step-by-step guidance.
---
This video is based on the question https://stackoverflow.com/q/63760055/ asked by the user 'Sachin Guruswamy' ( https://stackoverflow.com/u/11953431/ ) and on the answer https://stackoverflow.com/a/63760162/ provided by the user 'David C. Rankin' ( https://stackoverflow.com/u/3422102/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How can I extract a percentage number without the "%" symbol from a string?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Extract a Percentage Number from a String in Shell Scripts
Have you ever struggled with extracting a percentage number from a string in your shell scripts? Perhaps your string looks something like this:
[[See Video to Reveal this Text or Code Snippet]]
And all you want is to obtain the numeric value 100 from 100% without the percent sign. If you are finding it tricky to achieve this, you're not alone! In this guide, we’ll explore two effective methods to extract the percentage number using sed and awk.
Solution 1: Using sed
sed, a powerful stream editor, can be very effective for this task. Here’s how you can use it to extract the percentage value:
Step-by-Step Breakdown
Understanding the sed Command:
We will use a regular expression to match the pattern, capturing the digits before the % symbol.
The Command:
[[See Video to Reveal this Text or Code Snippet]]
How It Works:
^[^)]+ ): Matches characters from the start of the string until it finds a closing parenthesis ).
\s+ : Matches one or more whitespace characters following the parenthesis.
([0-9]+ )%: This capture group matches one or more digits followed by %.
.*$: Matches the rest of the line, discarding it.
Finally, \1 replaces the entire line with the captured digits.
Example Usage
You can test it out with the below command:
[[See Video to Reveal this Text or Code Snippet]]
Output:
[[See Video to Reveal this Text or Code Snippet]]
Solution 2: Using awk
If you prefer using awk, it provides a straightforward approach where you can loop through fields to find the percentage.
Step-by-Step Breakdown
Understanding the awk Command:
We will check each field until we find one that contains a % sign.
The Command:
[[See Video to Reveal this Text or Code Snippet]]
How It Works:
The command loops through each field (NF refers to the number of fields).
It checks if the field contains %.
If found, it uses sub() to remove everything from % to the end of that field.
Finally, it prints the modified field.
Example Usage
Run this command for another way to achieve the same result:
[[See Video to Reveal this Text or Code Snippet]]
Output:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
Both sed and awk are excellent tools for extracting numbers from strings in shell scripts. Whether you prefer the pattern matching capabilities of sed or the field processing approach of awk, you can impressively retrieve the percentage values you need.
Feel free to try these commands in your own shell scripts, and if you have any further questions or need additional clarification, don't hesitate to ask!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: