Bash String
29 AugustBash Replace Space with Newline
If you have multiple consecutive spaces in the string, use the last solution, the for loop. You can use any of the following solutions if you have a multiline string. Using tr Command Use the tr command to replace space with newline in bash. text="This is a sample string." new_text=$(echo "$text" | tr ' ' […]
26 NovemberCheck 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: 2023-11-24 […]
20 AugustCheck 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 JulyConvert 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. array=("Jan" "Feb" "Mar" "Apr") printf -v joined '%s,' "${array[@]}" echo "${joined%,}" Jan,Feb,Mar,Apr 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 […]
18 JulyGet Everything After Character in Bash
Using Parameter Expansion Use parameter expansion to get everything after character in Bash. string="Hello World!" delimiter=" " output="${string#*$delimiter}" echo "$output" World! 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 […]
18 JulyGet 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 Julysed 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. str=" It is an example line. " modified_str=$( echo "$str" | sed 's/^ *//;s/ *$//;') echo "|$str|" echo "|$modified_str|" | It is an example line. | |It is an example line.| […]
01 JulyBash Remove Spaces from String
Using sed Command Use the sed command to remove spaces from string without updating the original variable in Bash. var=" Java 2 Blog " echo $var | sed 's/ //g' Java2Blog First, we stored the string value in the var variable. Then, we piped it to the sed command, a stream editor performing operations on […]
30 JuneBash Return String from Function
Using Substitution Syntax Use substitution syntax represented by $(...) to return string from function in Bash. function return_string(){ echo "Welcome to Java2Blog" } str=$(return_string) echo "$str" Welcome to Java2Blog You can use local variable as well if you want as below: function return_string(){ local local_str_var="Java2Blog" echo "Welcome to $local_str_var" } str=$(return_string) echo "$str" Welcome to […]