Bash File
26 NovemberCheck 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: 2023-11-24 […]
02 AugustCheck 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. This is Line number 1. This is Line Number 2. It is Line number 3. I am Line 4. This is Line number 1. This is Line Number 2. It is Line number 3. I […]
24 JulyGet 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 […]
17 JulyPrint 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 […]
17 Julysed 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 […]
14 JulySort 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. […]
14 JulyBash 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 […]
03 Julysed 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 […]
01 JulyRemove 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 […]