Is this Ada textbook example rubbish? - ada

Here are the sources for the example programs of "Ada for software engineers":
http://www.springer.com/cda/content/document/cda_downloaddocument/978-1-84882-313-6_programs.zip
after extracting go to c04-arrays/justify
$ gnatmake justify.adb
Run justify, and compare output with "example.txt" which is the input.
The output I get is has intertwined lines and has nothing to do with justification.
If I save the output with:
$ justify > result.txt
then looking at that I can conclude that some lines where written ontop of others in the terminal.
But even if I ignore that issue, the result can hardly be called justification.
My question:
Do you get the same unexpected output?
Does the output you get resemble justification?
Note that the input is also supplied with the source! So I am not even trying to break the program.
the output I get on the terminal:
The quicke quick brown fox jumped over the lazy dog.
brown fox jumped over the lazy dog.
ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
The quickThe quick brown fox jumped over the lazy dog.
The quick brown foxed over the lazy dog.
The quick brown fox jumped over dog.
The quick brown fox jumped over the lazy
Thee slow gray wolf skipped over the frisky cat.
The slow grayray wolf skipped over the frisky cat.
The slow gray wolfed over the frisky cat.
The slow gray wolf skippedky cat.
The slow gray wolf skipped over the
The slow gray wolf skipped over the frisky
cat.
the output I have after redirection:
The quick brown fox jumped over the lazy dog.
The quick
brown fox jumped over the lazy dog.
ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
The quick brown fox jumped over the lazy dog.
The quick
brown fox jumped over the lazy dog.
The quick brown fox
jumped over the lazy dog.
The quick brown fox jumped over
the lazy dog.
The quick brown fox jumped over the lazy
dog.
The slow gray wolf skipped over the frisky cat.
The
slow gray wolf skipped over the frisky cat.
The slow gray
wolf skipped over the frisky cat.
The slow gray wolf
skipped over the frisky cat.
The slow gray wolf skipped
over the frisky cat.
The slow gray wolf skipped over the
frisky cat.
The slow gray wolf skipped over the frisky
cat.
Is this supposed to be justification?
Is this problem related to my machine/system/terminal/shell in particular, or is the problem somewhere else?

I get the same output on Mac OS X.
The problem is that example.txt is a Windows file, with CR/LF line endings, and you're running on a Unix system, which expects just LF; and the Ada RTS is leaving the CR at the end of each input line. (I suspect the C RTS might do the same; Python handles it better).
Lots of info in Wikipedia, including that you can use cat -v to see the control characters:
The quick brown fox jumped over the lazy dog.^M The quick
brown fox jumped over the lazy dog.^M
ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedov
The quick brown fox jumped over the lazy dog.^M The quick
brown fox jumped over the lazy dog.^M The quick brown fox
jumped over the lazy dog.^M The quick brown fox jumped over
the lazy dog.^M The quick brown fox jumped over the lazy
dog.^M The slow gray wolf skipped over the frisky cat.^M The
slow gray wolf skipped over the frisky cat.^M The slow gray
wolf skipped over the frisky cat.^M The slow gray wolf
skipped over the frisky cat.^M The slow gray wolf skipped
over the frisky cat.^M The slow gray wolf skipped over the
frisky cat.^M The slow gray wolf skipped over the frisky
cat.^M ^M
I don't know whether your system has a dos2unix utility, but in any case you can use tr:
$ tr -d '\r' <example.txt >example.lf
$ mv example.lf example.txt
Normally, you could have extracted the archive using unzip -a to convert line endings on all files that the original zip identified as text; in this case it identified all the files as binary, so you could use unzip -aa to force the conversion (given these are textbook examples, it's a fairly safe bet there won't be any actual binary files in there!)

The answer may be that your console is making tabs look like spaces, and thus giving different looking results.
You can test this with:
Ada.Text_IO.Put( ASCII.HT & '.' );
Ada.Text_IO.Put( ' ' & '.');
and see if the two periods are in the same column.

Related

Keep history of each tab separately even after close and reopen tabby terminal

I use tabby terminal and want to save history of each tab so every time my history is kept even after close/open. The good thing is when I write something in one tab and something else on another tab is only kept in those tabs means its good. The problem become when I close tabby and open again history of all tabs seems to be merged. How can I avoid that? This is what I have in my ~/.zshrc:
...
source $ZSH/oh-my-zsh.sh
unsetopt inc_append_history
unsetopt share_history
...

Is ZSH completion system capable of completing multiple arguments at the same time?

Scenario:
Let's say a script my_online_searcher <query> opens a browser with the search for query, where query can be multiple words long. This same script also provides a utility flag -s <query> that shows the search engine's suggestions. E.g.:
$ my_online_searcher -s lion rema
lion remake
lion remake cast
lion remake zoo tycoon 2
lion remake zt2
remake lion king
remastered lion's share
remake lion king 2019
remastered lion king
remake lion king trailer
remaking lion king
Desired outcome:
The user would type $ my_online_searcher lion rema[TAB] and ZSH completion menu would give the above options.
Attempts to achieve desired outcome:
Create a little completions script _my_online_searcher that pretty much calls the my_online_search -s <query> support function to give out options. Something like:
[...]
completions=(${(f)"$(my_online_searcher -s ${arg} ${words:2})"})
_describe 'suggestions' completions
[...]
This allows for the spaces to be escaped and allow ZSH to see the query as a single argument.
However, a per-word completion does not allow the progressive completion of the suggestions. Either it repeats the previous words, or filters out suggestions that do not begin with the first arguments (e.g. $ my_online searcher lion rema[TAB] -> $ my_online_searcher lion remake lion king).
One idea was to modify the LBUFFER inside the completion script. This turned out to not be allowed as ZSH gives the error that LBUFFER is readonly
TL;DR:
Is it possible to have ZSH completion system see all the arguments after the command as a modifiable argument? In other words, can I propose completions to multiple arguments at the same time in ZSH?

How to count occurrence 2 words at random places in a line in UNIX

Example (the file has following text):
tom is really really cool! joe for the win!
tom is actually smart.
tom and joe are best buddies. Joe is smart.
joe works hard
Now i want to find no of lines in this file that has joe and tom used in single line using UNIX.
so the answer should be 2 (ie first and forth lines only)
You can use plain grep with -c flag for counting the instances. For your input file,
grep 'tom.*joe\|joe.*tom' file
tom is really really cool! joe for the win!
tom and joe are best buddies. Joe is smart.
grep -c 'tom.*joe\|joe.*tom' file
2

Replace all spaces in a certain part UNIX

Hi all I'm trying to replace all spaces beginning in certain part of my file. I tried to do it but I can't make it to start in a certain part.
i tried this sed "s/\s/_/g" < file.txt > file_1.txt but all of the spaces turn into underscore.
inside file.txt :
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi ba Inu
Sibe rian Husky
Labra dor Retriever
Ger man Shep herd
Bull Doge
Be agle
chi hua hua
Bull Ter rier
expected file_1.txt:
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_I_nu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier
If you want the substitution to happen only after "List of Dogs", try
sed -e '1,/List of Dogs:/b' -e 's/\s/_/g'
The command b means "branch" (to the end of the script, i.e. bypass the substitution) and the address range specifies this action for the first line through the first line matching the regex.
If you want the substitution happen only after the :, use something like this:
sed -r '/:/,$ s/\s/_/g;' file.txt > file_1.txt
The substitution is restricted from a line containing : until the end of the file $.
Given your initial input file.txt:
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi ba Inu
Sibe rian Husky
Labra dor Retriever
Ger man Shep herd
Bull Doge
Be agle
chi hua hua
Bull Ter rier
You can try this:
$ sed '/List of Dogs/,$s/\s/_/g;s/List_of_Dogs/List of Dogs/g' file.txt
Which results:
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_Inu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier
Explanation
sed commands can be split by ;
first part starts with getting an address, which is the form range start,range end. Finds the line that List of Dogs starts at. And $ specifies last line of file, for the range end part of this syntax
so just for this address range, your search and replace command is done: $s/\s/_/g
but unfortunately the command also replaced and resulted in List_of_Dogs: so second command s/List_of_Dogs/List of Dogs/g is just a workaround to convert it back
You have the answer and you don't know it =)
You say you want to replace the spaces, but you have not said what you want to replace them with. I suspect, you want to replace them with a no-space character, right?
sed "s/ //g" $original_file > $new_file
or referencing the space with \s the following should also work
sed "s/\s//g" $original_file > $new_file
The syntax is basically
sed "s/find_this/replace_with/g" $original_file > $new_file
I hope that helps...
keep it simple, obvious, robust, portable, etc. and just use awk:
$ awk 'found{gsub(/[[:space:]]/,"_")} /:/{found=1} {print}' file
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_Inu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier

Transfer one line of text to another line in the same textfile

I would like to know if there is a particular code for tranfserring one line of a text to another line in the same text file in Unix? Supposedly i have Wow.txt and it contains:
The quick brown fox
jumps over the lazy dog
The dog is my pet
Oh yeah!
I would like to have an output of:
The quick brown fox jumps over the lazy dog
The dog is my pet
Oh yeah!
Is it possible? Thank you!
Try:
cat Wow.txt | tr -d '\n' > Wow-oneline.txt
Edit, or for slightly more clean, correct way
cat Wow.txt | tr -s '\n' | tr '\n' ' ' > Wow-oneline.txt
Edit x2:
If you're going to be doing any significant file processing, I would recommend reading up on sed and/or awk.
awk '!/^ *$/{print}' < Wow.txt | fmt

Resources