Bash
- 16 April
Bash Split String and Get Last Element
Using read Command Use the read command with parameter expansion to split the string and get the last element in Bash. [crayon-673e71fc701b0896513078/] [crayon-673e71fc701c2493396528/] In the above example, the read command is used with the ${myString##*:} parameter expansion to get the last element of the string which is then assigned to the lastElement variable using the […]
- 16 April
Add Comma to End of Each Line in Bash
1. Overview Adding characters like commas to the end of each line in a text file is a common operation in file processing and data manipulation. This task can be especially useful in formatting data files for CSV conversion or similar purposes. 2. Introduction to Problem Statement Imagine we have a text file named input.txt […]
- 16 April
Round to 2 Decimal Places in Bash
1. Overview In Bash scripting, dealing with numbers and specifically rounding them to a certain number of decimal places is a common task. For example, given a number like 3.14159, the goal is to round it to two decimal places, resulting in 3.14. This article explores various methods to achieve this in Bash, focusing on […]
- 16 April
Bash Log Output to File
Using Redirect Operator To log the output of a bash command to a file, use the redirect operator. There are two types of redirect Operator: > (Standard Output Redirect Operator): It redirects the output of a command to a file and overwrites the file’s contents. >> (Standard Output Append Operator): It appends the command’s output […]
- 16 April
Bash Remove Special Characters from String
Using Parameter Expansion Use the parameter expansion with regex to remove the special characters from a string in Bash. [crayon-673e71fcbebf1061125999/] [crayon-673e71fcbebfb499971351/] In this example, the parameter expansion removes all special characters from the "Hello World! This is a test string." string. We can observe that our input string contained two special characters, the exclamation marks […]
- 16 April
Remove Character from String in Bash
1. Overview In this article, we will explore different ways to remove characters in String in different scenarios such as removing specific characters, removing first character, removing last character, and removing first and last characters. 2. Using Parameter Expansion Bash’s built-in parameter expansion is best suited for performing string manipulation operations. Let’s explore parameter expansion […]
- 15 April
Remove Double Quotes from String in Bash
1. Overview Removing double quotes from strings is a s crucial task in many scenarios, such as parsing JSON data, cleaning up user input, or preparing strings for further processing. In this article, we will see different ways to remove double quotes from String using tr, parameter expansion, awk, sed and grep. 2. Introduction to […]
- 15 April
Replace Character in String in Bash
1. Overview In this article, we will explore different ways to replace characters in String in different scenarios such as replacing the first or all occurrences of a given character, replacing all occurrences of multiple characters, and substituting a complete word. 2. Using Parameter Expansion Bash’s built-in parameter expansion can be used to manipulate String […]