• 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. It is line1 It is line2 It is line3 It is line4 It is line5 awk '{print $NF}' input.txt line1 line2 line3 line4 line5 We use the awk command, a powerful text-processing tool allowing you […]

  • 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. date exit_code=$? echo […]

  • 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. print("Welcome to Java2Blog!") result=$(python my_script.py) echo $result Welcome to Java2Blog! 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 […]

  • 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. 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 […]

  • 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. 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.| […]

  • 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. This is 1st line This is 2nd line This is 3rd line This is 4th line This is 5th line This is 6th line This is 7th line This is 8th line This is 9th line This is 10th […]

  • 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. This is line 1. This is line 2. This is line 3. sed -i '$a\This is the last line' myFile.txt In this example, the sed command is used with the a option to add […]

  • Bash sort CSV by column
    14 July

    Sort CSV by Column in Bash

    Bash Sort CSV by Column Using sort Command Use bash’s sort command to sort the CSV file by column. Name,Age,City John,25,New York Alice,32,San Francisco Michael,28,Chicago Emily,22,Los Angeles #!/bin/bash sort -t',' -k2 myFile.csv Emily,22,Los Angeles John,25,New York Michael,28,Chicago Alice,32,San Francisco Name,Age,City In this example, the sort command is used to sort the myFile.csv file by column. […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.