• 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-662e1cefdc7c7583141747/] […]

  • 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-662e1cefddc2e183094716/] [crayon-662e1cefddc36256139989/] 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-662e1cefde46b264547140/] [crayon-662e1cefde471838535565/] 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-662e1cefdf3fa769723603/] [crayon-662e1cefdf3ff964453659/] 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-662e1cefdfb2b539308842/] [crayon-662e1cefdfb30713204796/] 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-662e1cefdff1a080603163/] [crayon-662e1cefdff1f472831566/] You can use local variable as well if you want as below: [crayon-662e1cefdff21216778696/] [crayon-662e1cefdff23229351851/] 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 […]