Evaluating shell commands in a file - linux

I have a file called foo.txt with contents:
Fusce porttitor tortor nisl, eu commodo mi mattis eu. Ut sodales,
nulla in facilisis ultrices, diam ante dapibus mi, at mollis
arcu ante vitae lectus. Aenean vitae bibendum massa, in varius est.
`cat baz.txt`
Mauris quis turpis venenatis, ultrices ipsum ac, mollis tellus.
I would like a shell command that I can invoke using
./cmd.sh foo.txt
that replaces each `-quoted shell command with the result of evaluating that command. How can this be done (ideally using Bash)?
As a bonus: I would like this to happen recursively. That is, if my baz.txt file contains further `-quoted shell commands, I would like those to be evaluated too.

The security worries expressed by #chepner notwithstanding, I found the following awk solution. It's not recursive, but it'll do.
awk -F"\"" '
$1=="SHELL " && NF>=2 {
system($2)
next
}
{print}' "$#"
The script looks for lines in the file of the form SHELL "foo", and replaces such lines with the result of executing "foo".

Related

Using grep to search for a word in a very long string

I have a huge file containing a unique long string. I need to search for a specific word in that file. Of course I cannot use gedit or similar software because they chock. So, a solution could be grep. The problem is that it returns the full string into the shell if the word matches, so I cannot find where the word is located and I cannot observe the other near words.
Is there any particular option to pass in order to stop/pause the grep shell stream (e.g., a certain number of chars after the match) as soon as it finds my word?
Use the -o option to "Show only the part of a matching line that matches PATTERN."
Example:
% cat lorem
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
% grep -oE '.{20}fugiat.{20}' lorem
se cillum dolore eu fugiat nulla pariatur. Exc
Edit: #tripleee suggested the E part, to give padding on either side of the match.
Use the -m NUM, --max-count=NUM option:
$ grep -m 1 [pattern] [/path/to/file]
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.

read and writing in tcl

Is there any way to read while it is writing at the same time in TCL?
I tried to use w + so and it didn't work.
set f0 [open out11.tr w+]
So I want to read every line that has been done writing at the same time
While the w+ mode will work, it does truncate the file when you open it (because it's a modification of the w mode which does the same thing). If you don't want to wipe the existing data, use r+ instead (in which case the file must exist first).
When you want to create the file if it doesn't exist, be able to read and write it through the same channel, and don't want to truncate it on open, you have to use the other form of mode descriptor (derived from POSIX descriptors, if you're interested in mnemonics):
set f0 [open out11.tr {RDWR CREAT}]
(w+ is RDWR CREAT TRUNC, r+ is plain RDWR.)
The following example opens a file with w+ (means read/write, but truncate the contents if file already exists). It then writes each line, and read back, write, then read back, ...
set lines {
{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi}
{accumsan est ut ante ornare et porta sem iaculis. Fusce a dolor}
{magna, eget viverra quam. In sem justo, hendrerit a porttitor sit}
{amet, scelerisque eu turpis. Nulla arcu arcu, condimentum vel}
{fermentum sit amet, vulputate et sapien. Aenean convallis, purus vel}
{molestie vehicula, diam eros adipiscing nibh, in dapibus nisi orci}
{ut nisl. Ut fermentum felis a lectus lacinia dapibus. Nunc cursus}
{nunc vitae massa fermentum imperdiet. In eu lectus quis arcu}
{convallis imperdiet in quis tortor.}
}
set f [open out.txt w+]
set lastRead 0
foreach line $lines {
# Write to the file
puts $f $line
# Read it back and display to stdout
seek $f $lastRead
gets $f line2
set lastRead [tell $f]
}
close $f

How to get a custom sentence format in VIM?

I use vim to write a lot of text (mostly for research papers), and I recently start to format paragraphs and sentences like this:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit sed diam et arcu scelerisque rutrum eget
vitae sed diam et arcu scelerisque rutrum eget
vitae sed diam et arcu scelerisque rutrum eget
vitae.
Aenean euismod tristique sollicitudin.
Vestibulum sed diam et arcu scelerisque rutrum eget
vitae sapien.
Quisque dui ligula, semper eget iaculis at, eleifend
at ligula.
Sed vestibulum tellus ac libero iaculis sit amet commodo
sapien pellentesque.
Cras quis dignissim neque.
Donec neque mauris, dictum tempus tincidunt in,
pellentesque sit amet dui.
I hope you can guess the pattern.
Now I do this mostly manual what is some kind of akward, especially if you add text in the middle of the sentence. My question would be, how can i do this automagically?
I know that I can use "gq100" to force a linebreak on the next 100 lines, but this does not do exactly what I want. It would be great if this is not really a hard linebreak but only a virtual one, which means when I put the cursor in the line starting with "Lorem ipsum" I can copy and past the whole sentence with y and p as if there wouldn't be any linebreak at all.
Does someone have an idea on this one?
Cheers,
T
Do you mean: If a line starts with 3 spaces, it belong to previous sentence?
You can do this:
:set textwidth=0
:set wrap
:set showbreak=\ \ \
Note: there's a space after every \.
Try to type a very lone line. It'll auto wrap. And the next line starts with 3 spaces.
I guess this is what you are looking for, the breakindent patch for vim:
http://sqizit.bartletts.id.au/2011/01/31/vim-and-breakindent/

VIM Spellcheck, Within Quoted Strings - PHP

I was wondering if it would be possible to run the vim spell checker while coding with PHP.
I can run it if I disable syntax highlighting, and it highlights almost all methods/function names because they are not English words.
So, my question is this, can I run the spell check on just the strings within PHP files?
For example, just within the ' quotes (and " quotes), ignoring everything else:
$paragraph = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est libero, dictum ut suscipit eget, lacinia in justo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent sit amet sem libero, in pretium enim. Pellentesque tortor ante, imperdiet quis mattis in, tincidunt et ligula. Cras porta velit a ligula venenatis placerat.';
$paragraph2 = "Vestibulum quis eleifend lectus. Vestibulum odio odio, mollis at eleifend a, adipiscing sed quam. Nam quis nisi quis mauris faucibus hendrerit eu a leo. Sed feugiat purus sit amet purus congue quis semper diam malesuada. Vivamus et enim non arcu pretium sollicitudin. Nullam blandit, lorem eu aliquet tincidunt, enim mauris placerat lectus, id faucibus nulla lectus non elit. Sed eget fermentum lacus. Ut tempus pulvinar neque, vel pellentesque purus aliquet quis. Vivamus molestie pretium quam at imperdiet. Maecenas diam turpis, malesuada ut sollicitudin ac, congue eleifend urna. Quisque id ipsum eget ipsum tempus blandit at vel magna. Fusce eu felis metus.";
see the vim help: :help spell-syntax
You can define particular syntax groups as having spell checking.
There's probably a better way to do it (using a ~/.vim/after/syntax/php.vim file?), but a simple hack is to make a personal version of the php syntax file in ~/.vim/syntax (you can do this by doing :e $VIMRUNTIME/syntax/php.vim and the :saveas ~/.vim/syntax/php.vim), and change all instances of the text contains=#phpAddStrings to contains=#Spell,#phpAddStrings. Next time you open a php file, turn on spell checking as normal with :set spell.

Latex: hfill to a certain width

I'm currently doing something like:
a\hfill{}b
which puts 'a' at the far left, and 'b' at the far right of the page. However, I'd like 'b' to be exactly half way. Is there a \hfill equivalent where I can say \hfill{0.5\textwidth}?
I don't know the width of 'a'.
Obviously, I could use tabular, or maybe minipage, but the rest of the code here is pretty complicated, so I'd rather not mess with it.
Edit:
a and b are both subfloats (subfigure) with lstinputlistings in them.
\documentclass{article}
\begin{document}
\noindent\hbox to 0.5\textwidth{a}b
Curabitur viverra ligula a dui condimentum tempus. Aenean convallis, massa ut fermentum
convallis, leo justo iaculis lectus, quis molestie orci ligula ut leo. Quisque vitae neque
sit amet tortor convallis rhoncus ac at nibh. Cras sit amet mauris sit amet neque
tincidunt iaculis non non ante. Proin quis nisi porttitor enim adipiscing bibendum et
fermentum magna. Morbi sapien nibh, venenatis in fringilla id, laoreet ut libero.
\end{document}
NB: this works if width of a is less than 0.5\textwidth so the multicol answer is probably more reasonable
a\hfill{}b\hspace{0.5\textwidth}
You could try a multicol environment with columnsep set to 0.
There is, by the way, also a way to measure the size of a rendered part, through \settowidth and \settoheight.
Try replacing \hfill to \hskip 0pt plus 1fill and you can do amazing things. For example,
a\hskip 0pt plus .35 fill b\hskip 0pt plus .65 fill

Resources