3 Likes
1 Shares
There is a way to get rid of the last, and only the last, newline in a file, if it exists. xxd
, in case you don't use it often, reads stdin
and shows you what was read, in hex.
printf "one\ntwo\nthree\n" > test.txt
cat test.txt | xxd
00000000: 6f6e 650a 7477 6f0a 7468 7265 650a one.two.three.
printf "%s" "$(cat test.txt)" | xxd
00000000: 6f6e 650a 7477 6f0a 7468 7265 65 one.two.three
This won't work with DOS-style newlines (\r\n
). The last \r
won't be eliminated. That would need a tr -d "\r"
, but that would eliminate all the \r
s.
If you knew for certain that there was a newline at the end, you could measure the length of the file with wc -c
and use head -c
to eliminate it. But this trick is so simple, I think I'd use it anyway with BASH and DASH.
I don't know if this trick works with any shells other than BASH and DASH. I think printf
is always a built-in command, so it would depend on the shell.
BTW, DASH is the variant form of BASH that Debian and Debian derivatives use. With Mint, commands you type from the command line, by default, use BASH, but scripts, by default, use DASH. This matters because echo
works slightly different with DASH and BASH. That's why some people use printf "%s\n" "whatever"
instead of echo "whatever"
in scripts.
#newline #newlines #bash #dash #shell #shell-script #trick #hack #programming #coding
Hey everyone, I’m #newhere. I’m interested in #archlinux, #coding, #darksouls, #emacs, #gaming, #gentoo, #irc, and #linux.
“Tough Developer” did this blog recently about looking back at Turbo C and Turbo C++, and also installed an old version to play around with it.
It is a reminder of how far we have come, firstly from the late 1980s and early 1990s, and secondly for those who remember coding with it back then, how advanced it was in its day. Coding before this time was “basically” (yes, I know) standalone text file editing (the days of Emacs and Vim), and you’d debug really from compiler errors.
I’d been programming in Quick Basic for DOS before this, and I only wrote one program in C++ using Borland C++, before I moved on to Visual Basic (with real GUI Windows), and Clipper, Python, etc.
Because there was no Internet yet, nor YouTube, I had to buy paper books to learn from (even though the Borland C++ package came in a massive, cubed foot size box with about 7 or 8 manuals). I still have the three books: Using Borland C++ 3, Tom Swan’s Code Secrets, and The Waite Group’s Turbo C++ Bible.
Maybe I just did not try a hard enough, but the whole C++ syntax never sat well with me. Yes, it wrote really nice tight code, but for some reason I never felt I could just flow with it.
It felt like, back then, that computing was really advancing in leaps and bounds every year or every second year. Graphics cards changed resolution and EGA colour came out, sound went from beeps to real true sound, spreadsheets appeared for the first time, the mouse appeared, graphical interfaces appeared (before Windows even), simulated multitasking appeared, the 640k memory barrier was broken (remember extended RAM and DR DOS?), we progressed from single sided floppy discs to double-sided, stiffy discs, token ring networks (with their T-connectors) appeared and were later replaced by Ethernet, 10MB MFM hard drives, and finally USB ports.
Today, graphics are already so good, storage is so abundant, processors so powerful, so most new innovations are just incremental in nature, and hardly noticed. As I heard on the LTT channel the other day, a user will notice moving from 60Hz refresh rate to a 120Hz monitor, but they will really struggle to notice improvements from 144Hz to 240Hz even though it is double the refresh rate.
Apart from a bit bigger and a bit better, the only big advancement that I remember from the last 15 years or so is SSD drives coming out (super light power, much more robust, and superfast). Even webcams and optical mice had already started to appear 20 to 24 years ago.
See https://www.codeproject.com/Articles/5358258/Revisiting-Borland-Turbo-C-Cplusplus-A-Great-IDE-b
#Blog, #BorlandC++, #coding, #retro, #technology
_
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.
You don't need a computer science background to participate - just a little programming knowledge and some problem solving skills will get you pretty far. Nor do you need a fancy computer; every problem has a solution that completes in at most 15 seconds on ten-year-old hardware.
#adventofcode #coding #linux #bsd #xterm #lua #gnulinux #html #python #ruby #perl #code
https://linuxiac.com/project-atom-archived-pulsar-is-the-way-forward/
Microsoft has Archived the ATOM editor, and the code repository on GitHub is no longer available.
Meet Pulsar, ATOMS successor, a free, community-led project built on Electron.
Hey everyone, I’m #newhere. I’m interested in #coding, #firefighter, #paramedic, and #tech. Thanks for the invite, @Welcome Diasporg!
I just posted this question on codereview.stackexchange.com
I'm building a GUI application with Python3+Pyside6 and have many QDialog
classes that are similar, and i was looking
for a way to avoid having to rewrite the same code when initializing them. The code below is one solution to this
This code creates a python decorator modal_dialog_init
to avoid having to write boilerplate code when initializing
classes with similar __init__
functions
(An alternative solution is to use inheritance, but then i could not have code both before and after: Putting the call
to super()
at the beginning or end of the __init__
function)
import sys
from PySide6 import QtWidgets
from PySide6 import QtCore
from PySide6 import QtGui
def modal_dialog_init(i_init_function):
def init_wrapper(self, *args, **kwargs):
super(self.__class__, self).__init__()
self.setModal(True)
self.vbox = QtWidgets.QVBoxLayout(self)
label = QtWidgets.QLabel("init_wrapper, before __init__")
self.vbox.addWidget(label)
i_init_function(self) # <-------------------
label = QtWidgets.QLabel("init_wrapper, after __init__")
self.vbox.addWidget(label)
for arg in args:
print(f"{arg}")
for k, v in kwargs.items():
print(f"{k}: {v}")
self.button_box = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Close, QtCore.Qt.Horizontal,
self
)
self.vbox.addWidget(self.button_box)
self.button_box.rejected.connect(self.reject)
self.adjustSize()
return init_wrapper
def start(cls, *args, **kwargs):
dlg = cls(*args, **kwargs)
dlg.exec()
return None
class AboutDialog(QtWidgets.QDialog):
@modal_dialog_init
def __init__(self, *args, **kwargs):
label = QtWidgets.QLabel("during __init__ --- About this application")
self.vbox.addWidget(label)
class SystemInfoDialog(QtWidgets.QDialog):
@modal_dialog_init
def __init__(self, *args, **kwargs):
label = QtWidgets.QLabel("during __init__ --- System info")
self.vbox.addWidget(label)
if __name__ == "__main__":
qapplication = QtWidgets.QApplication(sys.argv)
start(AboutDialog, "test-arg", param1="test-kw-arg-1", param2="test-kw-arg-2")
start(SystemInfoDialog, "another-test")
sys.exit(qapplication.exec())
Running the code:
pip install Pyside6
QDialog
classes. When the application is run the first is shown, and only once
that is closed is the next dialog shownI'm mainly interested in how this code can be improved in terms of maintainability, readability, elegance. And as a side
issue if there are other (better) ways of achieving the same thing (for example other design patterns)
Grateful for your thoughts on this!