• Bash format date to yyyymmdd
    25 July

    Format Date to yyyymmdd in Bash

    Using date Command Use the date command to format the date to yyyymmdd in Bash. [crayon-673f046b052f4794972284/] [crayon-673f046b05300419551924/] In the above example, the bash date command is used to get the current date with the format specifier '%Y%m%d' to specify the date format. In Bash, the date command is used to retrieve the date and time […]

  • Bash get last word in each line
    24 July

    Get Last Word in Each Line in Bash

    Using awk Command Use the awk command to get the last word of each line from a file in Bash. [crayon-673f046b0599a046017852/] [crayon-673f046b059a2271612347/] [crayon-673f046b059a4013839890/] We use the awk command, a powerful text-processing tool allowing you to process and manipulate structured data. The awk command operated each line individually and performed action based on the predefined patterns […]

  • Bash get exit code of last command
    24 July

    Exit Code of Last Command in Bash

    Every command we run successfully or unsuccessfully in Bash leaves a particular code behind, which we call an exit code or exit status. We use this code to analyze the last command in Bash. Using $? Variable Use shell variable, $?, to get the exit code of the last-run command in Bash. [crayon-673f046b05cd2305410253/] [crayon-673f046b05cd6894811262/] We […]

  • Bash get output from python script
    24 July

    Get Output from Python Script in Bash

    Using Substitution Syntax Use Substitution Syntax ($(...)) to get output from a Python script and store it in a Bash variable. [crayon-673f046b0606d939976456/] [crayon-673f046b06073521555525/] [crayon-673f046b06074458514986/] We used the command substitution syntax,$(command), to capture the output of the Python script (my_script.py) in a Bash variable (result). Here, the command was python my_script.py, where a Python interpreter executed […]

  • 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-673f046b063cf923894189/] [crayon-673f046b063d2161074592/] 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-673f046b0686b584794428/] [crayon-673f046b0686e237455854/] 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 get every nth line from File
    17 July

    Print Every nth Line from File in Bash

    Using awk Command Use the awk command to print every nth line from file in Bash. [crayon-673f046b06a2b456373532/] [crayon-673f046b06a2e663465952/] [crayon-673f046b06a30287638666/] [crayon-673f046b06a31729321166/] In this example, the awk command gets every 4th line from the file test.txt. Here, the file variable contains the file name from where you want to retrieve the lines. Then, the user is prompted […]

  • sed add line to end of file
    17 July

    sed Add Line to End of File

    Using sed with a command: Use sed with the a command to add a line to the file’s end in Bash. [crayon-673f046b06ccc858282419/] [crayon-673f046b06cd0083554331/] In this example, the sed command is used with the a option to add a line This is the last Line to the end of the file myFile.txt. In Bash, sed is […]