Single line create file with content - command-line

The OS is Ubuntu. I want to create file.txt in /home/z/Desktop where the content of the file is some text here.
The first and usual way is run nano /home/z/Desktop/file.txt and type some text here. after that, press ctrl+x, pressy followed by Enter.
The second way is run cat > /home/z/Desktop/file.txt, type some text here and press Enter followed by ctrl+c
I hope I can run single line of command to make it faster. I thought xdotool will work with the cat (the second way), but no, it not works

You can use "echo" in bash. e.g.:
echo "some text here" > file.txt
If you don't want a new line character at the end of the file, use the -n argument:
echo -n "some text here" > file.txt

Related

How to open STDIN in Sublime Text with syntax formatting?

I'm getting some JSON from curl and I want to open it using Sublime Text.
I've tried
curl ... | subl --command "Set Syntax: JSON" -
Data is opened, but syntax is not applied. Is my command syntax wrong?
Yes. The command subl expects is not the human readable text from the Command Palette, but the actual underlying command and arguments.
To see what these are, you could open the ST console (View menu -> Show Console) and type sublime.log_commands(True) Enter, then switch input focus back to the main file and set the syntax from the command palette. In the console, you'd see the command that was invoked.
Then, you'd end up trying something like (replacing echo[...] with your curl command):
echo '{ "test": 123 }' | subl --command 'set_file_type { "syntax": "Packages/JSON/JSON.sublime-syntax" }' -
...and it still wouldn't work. And you'd notice something odd if you had a non-JSON file focused previously. That file would now be highlighted as if it were JSON.
Why? Because it applies the command before it finishes opening the file (or in this case, opens a tab for the STDIN stream).
Can it be worked around? Yes, generally a separate invocation of subl to execute the command gives Sublime Text enough time to switch the focus to the newly created tab:
echo '{ "test": 123 }' | subl - && subl --command 'set_file_type { "syntax": "Packages/JSON/JSON.sublime-syntax" }'

Remove a specific word from a file using shell script

I would request some help with a basic shell script that should do the following job.
File a particular word from a given file (file path is always constant)
Backup the file
Delete the specific word or replace the word with ;
Save the file changes
Example
File Name - abc.cfg
Contains the following lines
network;private;Temp;Windows;System32
I've used the following SED command for the operation
sed -i -e "/Temp;/d" abc.cfg
The output is not as expected. The complete line is removed instead of just the word Temp;
Any help would be appreciated. Thank you
sed matches against lines, and /d is the delete directive, which is why you get a deleted line. Instead, use substitution to replace the offending word with nothing:
sed 's/Temp;//g' abc.cfg
The /g modifier means "globlal", in case the offending word appears more than once. I would hold off on the -i (inline) flag until you are sure of your command, in general, or use -i .backup.
Thank you. I used your suggestion but couldn't get through. I appreciate the input though.
I was able to achieve this using the following SED syntax
sed -e "s/Temp//g" -i.backup abc.cfg
I wanted to take the backup before the change & hence -i was helpful.

Compile text using command line compiler, not a file

I'm using luac -p file.lua to parse files to check for syntax errors. Is it possible to do something like this:
luac -p | [a bunch of text]
Someone mentioned something about 'piping' but I couldn't figure out how that would help.
What I'm wanting to do is take text from a program I am writing and put all that text into the compiler with -p so it just parses the text. Basically I want to check syntax of my program's textarea without having to write it to a file first.
In bash you can do
luac -p - << EOF
Then type your text. To indicate end, just type
EOF
on new line and press enter.

sed add line if not exists

I need to make a change in the php.ini configuration file via sed (or similar).
I need to add the following text:
extension=solr.so
The line has to be added as line number 941 in the configuration file. However, if the file is already there, it should not be added again.
I guess there are two approaches: 1) replace line 941 with the text, or 2) search for the text and add it to line 941 if there are not matches.
I have the following command that works fine, except that the line is added again if the script is run again:
sed '941i\
extension=solr.so' /etc/php5/apache2/php.ini > /etc/php5/apache2/php.ini
How can I make sure that this command does not add the line if it is already there?
The easiest way would be to test before using grep, for example:
grep -q -e 'extension=solr.so' file || sed '...'
Also, it is estrange that you need exactly that line. You should add it at the end, or something like that.
Also, note that taking the same file as input and output never should be done. This can damage the file badly. You should be using the -i sed parameter to do in-place editing.

Best way to open an URL in Vim

Lets say that you have either URL or a link on a webpage that is a text file. How would be the easiest way for the user to be able to open that file in a Vim?
Right click and save link as?
Use a specific file extension that defaults to Vim?
Command line command?
Depending on how your vim binary was built you can just give vim the url:
vim http://www.google.com/
Vim spawns curl and grabs the file, then opens it.
Assuming you want to just open a link in vim, how about this:
curl http://www.google.com | vim -
EDIT
to make this command easier you can always user your browser of choice's "Copy link address" option.
EDIT
Given #speshak's answer and my own, I would say the "easiest" way would be option 3, "a command line command".
Solution 1: use command
" gvimrc
for g:chrome_exe in [
\'C:\...\Google\Chrome\Application\chrome.exe',
\]
if !filereadable(g:chrome_exe)
continue
endif
command -nargs=+ URL :exe "silent !start ".g:chrome_exe." <args>"
break
endfor
Now when you type: :URL https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en
it will open google news
Solution 2: use function
or if you have a file that records a lot of URLs, and you want to use hotkey to open it, then you can try in this way
" .gvimrc
let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
normal "*yy
" let result = getreg("x")
" return result
:execute "silent !start ".g:chrome_exe2." ".getreg("*")
endfunction
map ,url :call OpenURL()<CR>
and then, you can open it with ,url
" test.txt
https://www.google.com/
https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en
Explanation of command
URL is a name, choose by you. (remember User-defined commands must start with an uppercase letter)
what is the command
command -nargs=+ Say :echo "<args>"
Now when you type :Say Hello World
Vim echoes "Hello World".
nargs
-nargs=0 No arguments
-nargs=1 One argument
-nargs=* Any number of arguments
-nargs=? Zero or one argument
-nargs=+ One or more arguments
I have used links before since RedHat days. The command would be
links http://www.google.com
If links is not installed, you can do sudo apt-get install links on Ubuntu 14.04 LTS to install it.
Hope it helps.