CLI editing text file
Removing last line:
You can use sed to remove the last line from a text file in the command-line interface.
The basic command is:
sed '$d' your_file.txt
sed: This is the stream editor command.'$d': This is the script passed tosed.$: This is an address that refers to the last line of the file.d: This is the command to delete the addressed line.
By default, sed prints the result to standard output and doesn’t modify the original file. To remove the line from the file itself, you need to use the in-place editing option, which is -i.
sed -i '$d' your_file.txt
Other Methods
While sed is a powerful tool for this, there are other common methods as well:
-
headcommand: Theheadcommand with the-n -1option prints all lines of a file except for the last one.head -n -1 your_file.txt > temp.txt && mv temp.txt your_file.txtThis method redirects the output to a temporary file (
temp.txt) and then renames it back to the original file name, effectively overwriting it. -
truncatecommand: This is a very fast method for large files, as it modifies the file in place without reading the entire file into memory. It works by calculating the length of the last line and then truncating the file by that many bytes.truncate -s "-$(tail -n 1 your_file.txt | wc -c)" your_file.txt
Delete last line from file in Unix
This video provides a practical demonstration of using the sed -i '$d' command to permanently remove the last line from a file in Unix.
http://googleusercontent.com/youtube_content/1
Writing to a file
The easiest way to write a line of text to a file in the terminal is by using the echo command.
Using echo to Write to a File
The echo command prints whatever text you give it. To send that text to a file instead of the screen, you use a special symbol called the redirection operator.
The >> Operator (Append)
This operator adds new text to the end of a file without deleting what’s already there.
Here’s a simple example:
echo "This is the first line." >> my_file.txt
This command adds the line “This is the first line.” to the file named my_file.txt. If the file doesn’t exist, it will be created for you.
You can run the command again to add another line:
echo "This is the second line." >> my_file.txt
Now my_file.txt will contain both lines.
Using > (Overwrite)
If you use a single > instead of >>, it will overwrite the entire file. This means any text that was in the file before will be deleted.
For example, if you ran the following command after the previous ones:
echo "This line will replace everything." > my_file.txt
The file my_file.txt would now only contain “This line will replace everything.” The two previous lines would be gone.
What about printf?
The printf command is also used to print text, but it’s more flexible and often used for more complex formatting. For simply adding a line to a file, echo is more direct and easier to use, which is why it’s the more common choice for this task.
For example, this command does the same thing as the echo example:
printf "This is a line from printf.\n" >> my_file.txt
The \n at the end of the text is a special character that tells printf to add a new line. Without it, the text would be added to the file, but it wouldn’t be on a new line.