Bash String
- 26 November
Check If File Contains String in Bash
1. Overview Searching for strings in text files is a common task in bash, used in scenarios like log file analysis and configuration file searches. This article explores various methods to check if file contains String, including both case-sensitive and case-insensitive approaches. 2. Introduction to Problem Statement Let’s consider a log file named server.log: [crayon-673ec9557c1dc803869884/] […]
- 20 August
Check If Output Contains String in Bash
1. Overview In this article, we will see how to check if output contains String in Bash using grep, Conditional Expressions, awk, sed commands with various options. 2. Introduction to Problem Statement We will use ls -l to list directories and files in long format and search for .txt string in the output. For example: […]
- 28 July
Convert Array to Comma Separated String in Bash
Using printf Statement Use the printf statement to convert the array to a comma-separated string in Bash. [crayon-673ec9557c826835570683/] [crayon-673ec9557c82a136877428/] We initialized an array containing string elements. Then, we used the printf statement to convert the specified array to a comma-separated string. How? The printf statement formatted and stored the elements of the array into the […]
- 18 July
Get Everything After Character in Bash
Using Parameter Expansion Use parameter expansion to get everything after character in Bash. [crayon-673ec9557d053897805866/] [crayon-673ec9557d05c395350791/] First, we set the string variable with a string type value; then, we initialised the delimiter variable with a space character (" "). The delimiter variable contained the character, after which we wanted to extract the remaining portion of the […]
- 18 July
Get Text Between Two Strings in Bash
1. Overview In scripting and programming, extracting specific portions of text from larger strings or files is a common task. In this article, we will see different ways to get text between two String using grep, awk, sed, and bash parameter expansion. 2. Introduction to Problem Statement We are given a String, and we need […]
- 18 July
sed Remove Leading and Trailing Whitespace
Using sed Command with Substitutions Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a single-line string in Bash. [crayon-673ec9557df72265699617/] [crayon-673ec9557df79981024949/] First, we created a variable named str and set its value to a string value which contained leading and trailing whitespaces. Then, we piped the echo $str command to […]
- 01 July
Bash Remove Spaces from String
Using sed Command Use the sed command to remove spaces from string without updating the original variable in Bash. [crayon-673ec9557e7e4534792326/] [crayon-673ec9557e7e9836807548/] First, we stored the string value in the var variable. Then, we piped it to the sed command, a stream editor performing operations on the text streams. In the above case, we used it […]
- 30 June
Bash Return String from Function
Using Substitution Syntax Use substitution syntax represented by $(...) to return string from function in Bash. [crayon-673ec9557ef71428852283/] [crayon-673ec9557ef76749109857/] You can use local variable as well if you want as below: [crayon-673ec9557ef77897076027/] [crayon-673ec9557ef78617231197/] In this example, the return_string() function initialized a local variable named local_str_var with the "Java2Blog" value. Then, we used an echo command to […]
- 30 June
Echo Multiple Lines in Bash
1. Overview In this article, we will see how to echo multiple lines, primarily using the echo command in various forms. Additionally, we will look at alternative methods like printf and here documents. 2. Introduction to Problem Statement Our goal is to print multiple lines with various options. The output of every method will be […]