• 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-6767759da6728017340184/] […]

  • Bash Check if Output contains String
    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: […]

  • Bash Convert Array to comma separated string
    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-6767759da6d60047347497/] [crayon-6767759da6d64161862325/] 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 […]

  • Bash get everything after character
    18 July

    Get Everything After Character in Bash

    Using Parameter Expansion Use parameter expansion to get everything after character in Bash. [crayon-6767759da6f55837026874/] [crayon-6767759da6f58710303235/] 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 […]

  • Bash get text between two Strings
    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 […]

  • sed remove leading and trailing whitespace
    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-6767759da73f1067933728/] [crayon-6767759da73f4139521049/] 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 […]

  • Bash remove spaces from String
    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-6767759da75df388423552/] [crayon-6767759da75e3499777982/] 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 […]

  • Bash return String from function
    30 June

    Bash Return String from Function

    Using Substitution Syntax Use substitution syntax represented by $(...) to return string from function in Bash. [crayon-6767759da78f9211219950/] [crayon-6767759da78fc949535548/] You can use local variable as well if you want as below: [crayon-6767759da78fd744296958/] [crayon-6767759da78ff399837764/] 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 […]

  • Bash echo multiple lines
    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 […]