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

  • 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-662e48dbcd7b5686847352/] [crayon-662e48dbcd7bf413047421/] [crayon-662e48dbcd7c0269917145/] Using cmp Command Use the cmp command to check if two files are the same in Bash. [crayon-662e48dbcd7c2367041934/] [crayon-662e48dbcd7c3154705004/] 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-662e48dbcde21985188672/] [crayon-662e48dbcde29591921944/] [crayon-662e48dbcde2b849986929/] 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-662e48dbceb82514554291/] [crayon-662e48dbceb8c179061417/] [crayon-662e48dbceb8e720518867/] [crayon-662e48dbceb90519216566/] 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-662e48dbcf6d1111329234/] [crayon-662e48dbcf6d9135403400/] 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-662e48dbcffb3863901298/] [crayon-662e48dbcffbb241231473/] [crayon-662e48dbcffbd448707708/] 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-662e48dbd0982223314270/] [crayon-662e48dbd098b226102811/] Use the echo command with the append operator (>>) to append a variable to the end of the specified file in Bash. [crayon-662e48dbd098d544518257/] [crayon-662e48dbd098f124240711/] 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-662e48dbd1373901418327/] [crayon-662e48dbd137c038647480/] [crayon-662e48dbd137e673807495/] 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 […]