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

  • Bash write variable to file
    14 July

    Bash Write Variable to File

    Using echo Command Use the echo command with redirection operator (>) to write variable to file in Bash. #!/bin/bash greetings="Welcome to Java2Blog!" echo $greetings > results.txt cat results.txt Welcome to Java2Blog! Use the echo command with the append operator (>>) to append a variable to the end of the specified file in Bash. #!/bin/bash greetings="Welcome […]

  • Bash Append to Array
    14 July

    Bash Append to Array

    Using Shorthand Operator Use the Shorthand operator to append one element to an array in Bash. #Declare an array my_array=("element1" "element2" "element3") #Append a new element my_array+=("new_element") #Print the array echo "${my_array[@]}" element1 element2 element3 new_element First, we declared an array (my_array) with three initial elements element1, element2, and element3. Then, we used the shorthand […]

  • Bash get curl response code
    05 July

    Bash Get Curl Response Code

    Using cUrl command with -w, -s and -o options For Get Method Use the -w or --write-out option in combination with the %{http_code} format specifier to get curl response code in Bash for get method. #!/bin/bash response_code=$(curl -s -o /dev/null -w %{http_code} 'https://www.google.com') echo "Response code: $response_code" Response code: 200 We can also rewrite it […]

  • sed remove line containing string
    03 July

    sed Remove Line Containing String

    Using sed Command Read the provided text file and remove lines containing string using sed command. We will not modify the input file (file.txt) but display the output on the Bash console. We can use -i option in case we want to modify input file This is line number one. It's line two. Here is […]

  • awk remove first column
    03 July

    awk Remove First Column

    Using awk command with sub() method Use the awk command with sub() method to remove the first column from the given CSV file (inputFile.csv) in Bash. We will not update the input file but display the output on the Bash console. index,firstname,lastname 1,John,Williamson 2,Nancy,Paul 3,Martin,Guptill 4,Adam,Douglass 5,Sam,Brown awk -F, '{$1="";sub(/,/,"")}1' OFS=, inputFile.csv firstname,lastname John,Williamson Nancy,Paul […]

  • Bash remove last line from file
    01 July

    Remove Last Line from File in Bash

    1. Overview In Unix-like environments, especially in scripting and programming with Bash, there might be scenarios where we need to remove the last line from a file. This operation is typical in data processing, log file management, and scripting. 2. Introduction to Problem Statement Imagine we have a text file named test.txt containing the following […]

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.