Using code script into a TIP or NOTE - asciidoc

I'd need to include some code sections into the TIP or [NOTE] section of my asciidoc. Something like this:
TIP: you can use the following shortcut to find the process id:
[source,shell]
----
jps -lv | grep -i myserver | cut -d ' ' -f 1
----
I've observed that the shell script is correctly rendered but it fails to display the TIP section. (It just writes TIP: as plain text).
Any clue?

Use the alternate admonition-block formatting and the -- block capture notation:
[TIP]
--
you can use the following shortcut to find the process id:
[source,shell]
----
jps -lv | grep -i myserver | cut -d ' ' -f 1
----
--
The [TIP] markup is just an alternative convention to the inline TIP: style. The opening and closing -- marks capture multiple blocks as part of the admonition block. This works with other types of blocks, sometimes in conjunction with +, as with ordered or unordered list items.

Related

How do I open a file in VS Code terminal by partially matching the file name?

If I have a file named w5_align_example.cpp, how do I open that file in VS Code integrated terminal by only supplying the word align?
code w5_align_sample.cpp would open it but I sometimes only remember the keyword align unless I search in a separate command to see what the file begins with. I want to open in a single command instead.
I've tried:
$ ls | grep "align" | code which gives me Run with 'code -' to read output from another program (e.g. 'echo Hello World | code -'). error.
$ ls | grep "align" | code - opens up a new file called code-stdin-sfd.txt with the text w5_align_example.cpp inside.
What would be the simplest (i.e. shortest) command to do this?
ls | grep "align" | xargs -I{} code {}
or
code $(ls | grep "align")
You can just use *. It matches any string and can be used multiple times.
code *align*
In some shells, you can combine this with tab completion. Just type:
code *align*
And then press Tab. This will fill in the rest of the file name, but it will beep if there is more than one option.

Defining a variable using head and cut

might be an easy question, I'm new in bash and haven't been able to find the solution to my question.
I'm writing the following script:
for file in `ls *.map`; do
ID=${file%.map}
convertf -p ${ID}_par #this is a program that I use, no problem
NAME=head -n 1 ${ID}.ind | cut -f1 -d":" #Now: This step is the problem: don't seem to be able to make a proper NAME function. I just want to take the first column of the first line of the file ${ID}.ind
It gives me the return
line 5: bad substitution
any help?
Thanks!
There are a couple of issues in your code:
for file in `ls *.map` does not do what you want. It will fail e.g. if any of the filenames contains a space or *, but there's more. See http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 for details.
You should just use for file in *.map instead.
ALL_UPPERCASE names are generally used for system variables and built-in shell variables. Use lowercase for your own names.
That said,
for file in *.map; do
id="${file%.map}"
convertf -p "${id}_par"
name="$(head -n 1 "${id}.ind" | cut -f1 -d":")"
...
looks like it would work. We just use $( cmd ) to capture the output of a command in a string.

Prepend message to rsstail

I am trying to prepend a message to the output of rsstail, this is what I have right now:
rsstail -o -i 15 --initial 0 http://feeds.bbci.co.uk/news/world/europe/rss.xml | awk -v time=$( date +\[%H:%M:%S_%d/%m/%Y\] ) '{print time,$0}' | tee someFile.txt
which should give me the following:
[23:46:49_23/10/2014] Title: someTitle
After the command I have a | while read line do ... end which never gets called because the above command does not output a single thing. What am I doing wrong?
PS: I am using the python version of rsstail, since the other one kept on crashing (https://github.com/gvalkov/rsstail.py)
EDIT:
As requested in the comments the command:
rsstail -o -i 15 --initial 0 http://feeds.bbci.co.uk/news/world/europe/rss.xml
Will give back a message like the following when a new article is found
Title: Sweden calls off search for sub
It seems that my rsstail is different from yours, but mine supports the option
-Z x add heading 'x'
so that
rsstail -Z"$( date +\[%H:%M:%S_%d/%m/%Y\] ) " ...
does the job without awk; on the other hand, you do have some problem with buffering, is it possible to ask rsstail to stop after a given number of titles?

use zsh's built in pager instead of less

Suppose I've got a giant command
echo "start string `complexcommand -with -many args | cut -d ' ' -moreargs | sed 's/you/get/g' | grep -v "the idea" | xargs echo` ending string" | program | less -S
It produces output of several hundred lines of many thousand characters in length.
less handles scrolling vertically quite well, as that's what it is used for most of the time, but scrolling left and right is very CPU taxing according to top and I am not aware of any "page-left" or "page-right" style commands to go faster.
So I'm hoping that something like zsh's built-in pager could handle this task faster, but I'm having trouble figuring out the command to use it. It takes a file input. Is there a way to make a oneliner use the pager rather than having to dump it to a file first?
Or if anybody has suggestions for better editors. I might try using vim next.
If you want to invoke zsh's pager, use some-complex-pipeline | zsh -c '< /dev/fd/0'. The /dev/fd/0 file is a device that represents the current process's standard input stream.

gnu watch: justify on the lower left of the terminal

I want to apply a watch command on a mysql query every N seconds, but would like to have the results on the bottom left of the terminal instead of the top left:
watch -n 120 "mysql_query" | column -t"
Shows my results like so:
--------------------------
|xxxxxxxxxxx |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
| |
| |
--------------------------
Whereas I would like them to have like so:
--------------------------
| |
| |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
--------------------------
Suggestion?
I don't see a straight-forward way to do this, but I managed to force it to work using the following approach. I haven't fully tested this so I cannot guarantee that this will work in all situations.
Using this script:
#!/bin/bash
TERM_HEIGHT=`tput lines` # determine terminal height
WATCH_BANNER_HEIGHT=2 # account for the lines taken up by the header of "watch"
let VIS_LINES="TERM_HEIGHT - WATCH_BANNER_HEIGHT" # height of visible area
(yes " " | head -n $VIS_LINES; cat | head -n $VIS_LINES) | tail -n $VIS_LINES
Post process the output of your command as it is called by watch e.g. (assuming the script was saved as align_bottom, made executable, and store somewhere within your $PATH):
watch -n 120 "mysql_query | column -t | align_bottom"
What the script does:
Determine the height (number of lines) of the terminal
Calculate the visible area of the watch output
Print blank lines to pad the output (pushing the output down)
Read in output from stdin, and trim it so we only show the top of the output if it extends beyond the screen. If you want to see the bottom of the output instead, simple remove the head command after cat.
tail the output of steps (3) and (4) so excess padding is removed and the final output fits snugly within watch
I have to admit this seems a little hackish, but hopefully it gets you closer to what you're trying to achieve.
Update:
It should also be possible to implement that as a function instead just so it can sit comfortably in .bashrc.
function align_bottom() {
(( VIS = $(tput lines) - 2 )) # height of visible area
(yes " " | head -n $VIS; cat | head -n $VIS) | tail -n $VIS
}
typeset -fx align_bottom # !! make it callable from subshell
Usage would be the same:
watch -n 120 "mysql_query | column -t | align_bottom"
Note that watch runs the given command using sh -c, therefore, as Dennis pointed out in the comments, on systems that does not link /bin/sh to /bin/bash the function approach shown above will not work.
It is possible to make it work usign:
watch -n 120 "mysql_query | column -t | bash -c align_bottom"
but for portability and usability, it's cleaner to simply use the shell script approach.
I don't know if watch can do that, but what I'd do is use another tool to have multiple terminals and resize the one in which watch is running according to my needs.
A couple of these tools that can be useful are:
screen
byobu (screen with some enhancements)
terminator
I hope this helps.

Resources