How to create an article from the command line

Introduction

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.

Let's extract sentences from the Web

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

Let's output in the required format

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'

Search and extract the necessary part

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

How to create an article from the command line
[EC2] How to install and download chromedriver from the command line
How to measure line speed from the terminal
(Remember quickly) How to use the LINUX command line
How to create an email user
How to pass arguments when invoking python script from blender on the command line
How to create a clone from Github
How to create an NVIDIA Docker environment
[Linux] How to use the echo command
How to get a string from a command line argument in python
How to use the Linux grep command
How to operate Linux from the console
How to extract the desired character string from a line 4 commands
Create custom Django commands and run them from the command line
How to create a repository from media
How to access the Datastore from the outside
How to build an application from the cloud using the Django web framework
[Python] How to test command line parser click
How to receive command line arguments in Python
How to create a function object from a string
How to create an OCF compliant resource agent
How to operate Linux from the outside Procedure
How to create a shortcut command for LINUX
How to create a Kivy 1-line input box
Convert XLSX to CSV on the command line
Create a command to get the work log
An article summarizing the pitfalls addicted to python
[Python] How to remove duplicate values from the list
How to write a GUI using the maya command
How to delete the specified string with the sed command! !! !!
Open Chrome version of LINE from the command line [Linux]
[Blender x Python] How to create an original object
How to create a submenu with the [Blender] plugin
Backtrader How to import an indicator from another file
Linux user addition, how to use the useradd command
How to use the grep command and frequent samples
How to instantly launch Jupyter Notebook from the terminal
How to create an image uploader in Bottle (Python)
How to hit the document of Magic Function (Line Magic)
How to post a ticket from the Shogun API
Let's add it to the environment variable from the command ~
How to create a kubernetes pod from python code
sphinx-quickstart got messy and I tried to create an alternative command and the stress disappeared
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to use the generator
Twitter post from command line
How to use the decorator
How to increase the axis
How to start the program
How to return to the previous directory with the Bash cd command
Command line tool to put .gitkeep in an empty directory
Search for large files on Linux from the command line
How to generate a public key from an SSH private key
How to manipulate the DOM in an iframe with Selenium
How to calculate the amount of calculation learned from ABC134-D
How to log in automatically like 1Password from the CLI
How to do the initial setup from Django project creation
How to specify command line arguments when debugging in PyCharm
How to hit NAPALM from the Web (NetDevOpsSec reality solution)
[Python Kivy] How to create an exe file with pyinstaller
How to create an ISO file (CD image) on Linux