Use sponge utility to read standard input and safely write it out to the same file.

Prerequisites

Install moreutils package to use sponge utility.

$ sudo apt install moreutils

Case #1

Inspect fstab configuration file.

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/vgubuntu-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=45aa534e-49f6-4593-9257-64de25d2da14 /boot           ext4    defaults        0       2
/dev/mapper/vgubuntu-swap_1 none            swap    sw              0       0

Use it alter file contents, for example remove swap file system.

$ grep -v swap /etc/fstab | sudo sponge /etc/fstab

Inspect fstab configuration file again, swap file system was removed.

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/vgubuntu-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=45aa534e-49f6-4593-9257-64de25d2da14 /boot           ext4    defaults        0       2

Case #2

Inspect sample log file.

$ cat /var/log/kern.log.20201011
[...]
Oct 11 16:13:34 desktop kernel: [3325726.332658] blk_update_request: I/O error, dev sdc, sector 244514032 op 0x0:(READ) flags 0x80700 phys_seg 2 prio class 0
Oct 11 16:13:34 desktop kernel: [3325726.334528] blk_update_request: I/O error, dev sdc, sector 244513792 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
Oct 11 16:13:34 desktop kernel: [3325726.334531] Buffer I/O error on dev sdc1, logical block 30560128, async page read
Oct 13 19:01:50 desktop kernel: [3508622.151379] audit: type=1400 audit(1602608510.241:470): apparmor="STATUS" operation="profile_replace" info="same as current profile, skipping" profile="unconfined" name="snap-update-ns.terraform" pid=374426 comm="apparmor_parser"
Oct 13 19:01:50 desktop kernel: [3508622.159218] audit: type=1400 audit(1602608510.249:471): apparmor="STATUS" operation="profile_replace" info="same as current profile, skipping" profile="unconfined" name="snap.terraform.terraform" pid=374427 comm="apparmor_parser"
[...]

Truncate it, preserve original user/group and keep only I/O error messages

$ grep -i "i/o error" /var/log/kern.log.20201011 | sudo -u syslog -g adm sponge /var/log/kern.log.20201011

Log file was altered.

$ sudo cat /var/log/kern.log.20201011
Oct 11 16:13:34 desktop kernel: [3325726.332658] blk_update_request: I/O error, dev sdc, sector 244514032 op 0x0:(READ) flags 0x80700 phys_seg 2 prio class 0
Oct 11 16:13:34 desktop kernel: [3325726.334528] blk_update_request: I/O error, dev sdc, sector 244513792 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
Oct 11 16:13:34 desktop kernel: [3325726.334531] Buffer I/O error on dev sdc1, logical block 30560128, async page read

Reverse this log file.

$ tac /var/log/kern.log.20201011 | sudo -u syslog -g adm sponge /var/log/kern.log.20201011

Lines are in reverse order.

$ cat  /var/log/kern.log.20201011
Oct 11 16:13:34 desktop kernel: [3325726.334531] Buffer I/O error on dev sdc1, logical block 30560128, async page read
Oct 11 16:13:34 desktop kernel: [3325726.334528] blk_update_request: I/O error, dev sdc, sector 244513792 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
Oct 11 16:13:34 desktop kernel: [3325726.332658] blk_update_request: I/O error, dev sdc, sector 244514032 op 0x0:(READ) flags 0x80700 phys_seg 2 prio class 0

Case #3

Look at this simple temperature log file.

$ cat temp.log
#${TEMPLATE_DATE}:${TEMPLATE_TEMP}
1600133692:24
1600937387:27
1601132241:23
1602135324:25
1603131093:28

Use sponge and envsubst utilities to append new entry.

$ grep TEMPLATE temp.log | TEMPLATE_DATE=$(date +%s) TEMPLATE_TEMP=22 envsubst | tr -d '#' | sponge -a temp.log

New entry was added to the log file.

$ cat temp.log
#${TEMPLATE_DATE}:${TEMPLATE_TEMP}
1600133692:24
1600937387:27
1601132241:23
1602135324:25
1603131093:28
1603135757:22
ko-fi