• 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-673ed1e51dfac823342699/] […]

  • Bash Check if two files are same
    02 August

    Check if Two Files are the Same in Bash

    In this entire article, we will use the following content of three different text files to practice the provided solutions. [crayon-673ed1e51e40b321802555/] [crayon-673ed1e51e40f298894489/] [crayon-673ed1e51e410195609594/] Using cmp Command Use the cmp command to check if two files are the same in Bash. [crayon-673ed1e51e411342233589/] [crayon-673ed1e51e412727644598/] In the above example, we initialized the file1 and file2 variables with the […]

  • 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-673ed1e51e7d5907952154/] [crayon-673ed1e51e7d8022967997/] [crayon-673ed1e51e7d9435795345/] 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 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-673ed1e51ea7b028813569/] [crayon-673ed1e51ea7e630187193/] [crayon-673ed1e51ea7f323102103/] [crayon-673ed1e51ea80306986203/] 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-673ed1e51ec90862004570/] [crayon-673ed1e51ec93840554732/] 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 […]

  • 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. [crayon-673ed1e51edfa714592791/] [crayon-673ed1e51edfe146858901/] [crayon-673ed1e51ee00299052589/] In this example, the sort command is used to sort the myFile.csv file by column. Here, the -t parameter is used to specify the field separator, which is , in the above case. […]

  • 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. [crayon-673ed1e51fbbd573683783/] [crayon-673ed1e51fbc3183678101/] Use the echo command with the append operator (>>) to append a variable to the end of the specified file in Bash. [crayon-673ed1e51fbc7970664970/] [crayon-673ed1e51fbc8819004435/] Use the echo command with the append operator (>>) to append […]

  • 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 [crayon-673ed1e51fe45794027088/] [crayon-673ed1e51fe49083885750/] [crayon-673ed1e51fe4a603233549/] Let’s break down command to understand it […]

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