This content originally appeared on Go Make Things and was authored by Go Make Things
Today, we’re looking at how to log and print things in Terminal.
Note: I’m wrapping up production on a new series of courses on developer tooling. The first course on Terminal should be ready in the next week or two, but you can pre-order it today. This is an excerpt from the guide.
Printing things in Terminal
Use the echo command to print things in the Terminal window. Whatever you type after echo is printed.
# prints "Ahoy, matey"
echo Ahoy, mateyYou can also wrap the text to log in quotes.
# also prints "Ahoy, matey"
echo "Ahoy, matey"You can use \n to add a line break. This only works if your text is wrapped in quotes.
echo "Ahoy\nmatey"Note: in Terminal, certain characters, like the bang operator (!) are used with commands. If included in an echo string, they can have unintended side-effects unless you escape them with a backslash (\) like this: echo "Ahoy, matey\!".
Reading the contents of a file
Use the cat command to read the contents of a file and print it into the console. Use the filename as an argument.
# prints the contents of the characters.md file
cat characters.mdHere, the full contents of the characters.md file are printed into the Terminal window.
Wizard
Druid
Knight
BardIf you use the -n option before your filename, line numbers are included.
# print the character.md file with line numbers
cat -n characters.mdFile names can be wrapped in quotes. You can also pass in multiple files as arguments.
# print the contents of two files
cat "characters.md" "pirates.md"
# print multiple files with line numbers
cat -n "characters.md" "pirates.md"Sorting the contents of a file
Use the sort command to sort the contents of a file alphabetically, in reverse order, by number, or by month. It can also remove duplicates.
By default, spaces are treated as the delimiter. The original file is not modified.
For example, let’s imagine that we have a characters.md file that looks like this. The character types are not in any sort of order.
Wizard
Druid
Knight
BardTo sort them alphabetically, we would do this.
# sort the contents of the characters.md file alphabetically
sort characters.mdThis is what’s printed to the console as a result.
Bard
Druid
Knight
WizardYou can sort in reverse order using the -r option.
# sort the contents in reverse alphabetical order
sort -r characters.mdTo sort by numeric order, use the -n option. You can also sort in reverse-numeric order using -n and -r together.
For example, imagine that you have a health.md file that tracks an RPG characters health.
12 - Wizard
11 - Druid
20 - Knight
4 - BardTo sort it numerically (in ascending or reverse order, respectively), you would do this.
# sort in numeric order
sort -n health.md
# sort in reverse numeric order
sort -nr health.mdTo remove duplicates while sorting, use the -u option.
For example, imagine you have a more-characters.md file with some duplicates in it.
Wizard
Druid
Knight
Bard
Druid
Wizard
Healer
BarbarianTo sort them and remove the duplicates, you would do this.
# remove duplicates
sort -u more-characters.mdYou can use the -o option to save the sorted output to a file. Add it to the end of your command, other options, and argument, with the filename as an option value.
# sort alphabetically and save to a new file
sort -u more-characters.md -o characters-sorted-unique.mdClearing the Terminal window
The clear command will remove all content from the Terminal window.
# clear the Terminal window
clearLiked this? Get 15 FREE self-paced JavaScript projects. Each one includes a few short lessons and a template to get you started. Click here to get started.
This content originally appeared on Go Make Things and was authored by Go Make Things
Go Make Things | Sciencx (2022-08-26T14:30:00+00:00) Logging and printing things in Terminal. Retrieved from https://www.scien.cx/2022/08/26/logging-and-printing-things-in-terminal/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.