You can use the command line to blog or Qiita, but posting some text is surprisingly easy, so I think it would be convenient to create such a mechanism.
The procedure of opening a browser, copying it, and writing it down in an editor when there is something to investigate is one of the most troublesome things for a CLI or CUI user. This time, I would like to introduce some personal tips that are useful in such cases.
By the way, CLI (command line user interface) and CUI (character user interface) are used interchangeably here. In other words, it would be nice if you could imagine it as a black screen, but specifically, you do it on a terminal (Terminal). I wonder if I often call it CUI. Because it can be used as a pair with the GUI. The title uses CLI.
A Python tool called html2text
is very useful for fetching the text you want from the web. This will display the HTML in text format. Therefore, the output of wget
and curl
can be used as it is, which is easy.
python
$ pip install html2text
$ curl -sL http://qiita.com/syui/items/ab6a11aa57df8590d645 | html2text
Use peco
to extract a sentence. It's a golang tool, but you can choose the output you want.
python
$ go get github.com/peco/peco/cmd/peco
$ curl -sL http://qiita.com/syui/items/ab6a11aa57df8590d645 | html2text | peco
By the way, peco
and html2text
are very easy to use, so it may be good to set global alias etc.
~/.zshrc
alias -g P='|peco'
alias -g H='|html2text'
alias -g J='|jq'
Now you can use it as follows.
python
# ls -A | peco
$ ls -A P
Jq
is useful for people who take notes in JSON. Personally, Qiita posts and posts to blogs are all automatically made and posted in the prescribed format of notes taken in JSON, but for example, articles like this I will make up.
python
$ sudo pacman -S jq
json
and jq
are easy once you get used to them, but if you don't get used to them, it will be difficult to use at first. For the time being, I will introduce some commands that can be used as a reference. You can retrieve the value like this.
hoge.json
[
{
"title": "json",
"tags": [ { "name": "json" }, { "name":"text"}, { "name":"golang"}, { "name":"html"}, { "name":"python"} ],
"body": "hello world",
"coediting": false,
"gist": false,
"private": false,
"tweet": false
}
]
python
#Extract the entire value
$ cat hoge.json | jq .
#Fetch values without conversion, parentheses"Get rid of
$ cat hoge.json | jq -r .
#brackets[]Take out the inside for the time being
$ cat hoge.json | jq '.[]'
#brackets{}Specify the number in the order of
$ cat hoge.json | jq '.[0]'
#brackets{}Extract by specifying the value of title in
$ cat hoge.json | jq '.[].title'
$ cat hoge.json | jq '.[]|.title'
#brackets[]Only the value in parentheses"Take out everything except
$ cat hoge.json | jq -r '.[]|.[]'
#Take out all tags
$ cat hoge.json | jq '.[].tags|.[].name'
#Extract by specifying the number of tags
$ cat hoge.json | jq '.[].tags|.[1,2].name'
Sometimes you want to search and retrieve the text you need. In such a case, it is convenient to create the following script.
Use it like $ ./search.sh arch linux json
.
search.sh
#!/bin/zsh
startpage="https://startpage.com/do/search?q="
case $1 in
*)
if [ ! $# = 0 ];then
keyword=${startpage}`echo "${@}"|tr ' ' '+'`
echo $keyword
one_step=`curl -sL ${keyword} | html2text | grep '###' | peco |cut -d"(" -f2 | cut -d")" -f1`
two_step=`curl -sL ${one_step} | html2text | peco`
case $OSTYPE in
darwin*)
echo "$two_step"| pbcopy && pbpaste
;;
linux*)
echo "$two_step"| xclip -i -sel c && xclip -o -sel c
;;
esac
else
echo '
g <hoge> : google.com/search?q=<hoge>
'
fi
;;
h|-h|-[hH]elp|--[hH]elp)
echo '
g <hoge> : google.com/search?q=<hoge>
'
;;
esac
'
Recommended Posts