#75 使用 jq 命令解析 JSON 数据

2021-03-04

JSON Logo

jq 是我在命令行中解析 JSON 的一个常用工具,用起来非常顺手。

  • https://github.com/stedolan/jq
  • https://stedolan.github.io/jq/

用法

curl https://24pullrequests.com/users.json | jq

# 取第一个元素
curl https://24pullrequests.com/users.json | jq '.[0]'
# 取第一个元素的指定字段
curl https://24pullrequests.com/users.json | jq '.[0].nickname'

# 切片
curl https://24pullrequests.com/users.json | jq '.[:2]'

# 遍历
curl https://24pullrequests.com/users.json | jq '.[] | .nickname'

# 取字段
curl https://24pullrequests.com/users/changeworld.json | jq .nickname

# 取多个字段
curl https://24pullrequests.com/users/changeworld.json | jq '.nickname,.contributions_count'

# 获取列表长度
curl https://24pullrequests.com/users.json | jq length

# 列出 Keys
curl https://24pullrequests.com/users/changeworld.json | jq keys
curl https://24pullrequests.com/users/changeworld.json | jq "keys[]"
curl https://24pullrequests.com/users.json | jq ".[0] | keys[]"

# 列出 Keys 和 值类型
cat a.json | jq ".[0] | keys,map(type)"
cat a.json | jq ".[0] | to_entries | map([.key, (.value | type)])"
cat a.json | jq '.[0] | to_entries | map("\(.key) : \(.value|type)")[]'

还有很多更强大的用法,可以参考文档,我就会上面几个,在命令行中简单搜索 JSON 也够用了。

刚在文档中学会一招,重新组合 JSON:

curl https://24pullrequests.com/users/changeworld.json | jq '[.nickname, .organisations[].login]'
curl https://24pullrequests.com/users/changeworld.json | jq '{name:.nickname, orgs:[.organisations[].login]}'

👍🏻 Nice!!!

#73 高效搜索日志

2020-07-22

即便上了 ELK,也不可能所有日志往里面塞,搜日志是程序员的日常工作之一。

grep log by time efficiently 为关键词搜到 SO 上的这个问题: What is the most efficient way to extract logs between two time stamps?

要求:

  1. 能够过滤出指定时间段日志
  2. 效率,保证即便是大日志也不至于等太久,减少对服务的不良影响

提问的人自己提出来的方案:

grep -a -A 1000000 "03/09" fileName.txt | grep -a -B 1000000 "03/10"

他认为不好,因为有点慢,而且有时会漏掉一些日志。
漏掉日志可能和他的日志格式有关,这倒应该问题不大,主要是这个方案看起来就确实效率不行。
PS:如果不用模式匹配,可以加上 -F,应该可以提高效率。

# 2020/07/22 10:00:00,100 INFO [main] com.example.LoggingApplication - Starting Application
awk '$2>="16:30:00"{s=1} s; $2>="17:00:00"{exit}' event.log

这个看起来很牛逼,主要是有一个 exit

#72 Shell 转义

2020-07-21

这篇文章不是要记录那些乱七八糟的转义规则,而是讲怎么借助工具避开它。

GitHub 上偶然看到一个仓库 chrissimpkins/shellescape,做了些实验之后,发现非常好用。
别看就几行代码,真的感觉世界都清静了。
PS:代码我没细看,人生短暂,我不想去记这些没用的知识点。

import re

_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search

def quote(s):
    if not s:
        return "''"
    if _find_unsafe(s) is None:
        return s
    return "'" + s.replace("'", "'\"'\"'") + "'"

示例:

commands = """
grep -F '"userId": "1"' /tmp/test.log
grep 'aaa.bbb.$100.*' /tmp/test.log
""".strip().splitlines()
for command in commands:
    print(quote(command))

#71 Shell 小技巧

2020-07-20

快速输入上一行命令的最后一个参数

touch nihao.txt
code !$

创建目录并进入

take xxx # oh-my-zsh

mkdir xxx && cd $_

重命名

for i in {0..100}; do touch aaa_bbb_$i.jpg; done
rename 's/_bbb//' aaa_*.jpg

# for zsh
autoload zmv
zmv -n '(*)_(*).jpg' '$2_$1.jpg'

#70 Linux 查看文件打开情况

2020-06-23

查看这个进程打开了哪写文件

-> % ps -ef | grep rsyslogd | grep -Fv grep
syslog      1068       1  0 1月06 ?       00:00:03 /usr/sbin/rsyslogd -n -iNONE

-> % sudo ls -l /proc/1068/fd 
总计 0
lr-x------ 1 root root 64  1月  8 16:54 0 -> /dev/null
l-wx------ 1 root root 64  1月  8 16:54 1 -> /dev/null
l-wx------ 1 root root 64  1月  8 16:54 10 -> /var/log/kern.log
l-wx------ 1 root root 64  1月  8 16:54 11 -> /var/log/auth.log
l-wx------ 1 root root 64  1月  8 16:54 2 -> /dev/null
lrwx------ 1 root root 64  1月  6 09:46 3 -> 'socket:[13299]'
lr-x------ 1 root root 64  1月  8 16:54 4 -> /dev/urandom
lrwx------ 1 root root 64  1月  8 16:54 5 -> 'socket:[27825]'
lrwx------ 1 root root 64  1月  8 16:54 6 -> 'socket:[27831]'
lr-x------ 1 root root 64  1月  8 16:54 7 -> /proc/kmsg
lrwx------ 1 root root 64  1月  8 16:54 8 -> 'socket:[22468]'
l-wx------ 1 root root 64  1月  8 16:54 9 -> /var/log/syslog

查看哪些进程打开了这个文件

-> % sudo find /proc/*/fd -ls | grep /var/log/syslog
  2551982      0 l-wx------   1 root             root                   64 1月  8 16:50 /proc/1068/fd/9 -> /var/log/syslog

-> % ps -fq 1068   
UID          PID    PPID  C STIME TTY          TIME CMD
syslog      1068       1  0 1月06 ?       00:00:03 /usr/sbin/rsyslogd -n -iNONE

#69 Ubuntu 快捷键

2020-03-31

全局

Win / Win + S 窗口概览
Win + A 程序搜索,或者 Win 双击
Win + D 显示桌面
Win + L 锁定窗口
Win + V / Win + M 打开日历和事件
Win + 数字 打开左侧菜单上的程序
Win + Left 将窗口移动到左侧
Win + Right 将窗口移动到右侧
Win + Up 最大化
Win + Down 还原
Win + Home 切换到第一个 Workspace
Win + PageUp 切换到上一个 Workspace
Win + PageDown 切换到上一个 Workspace
Alt + F2 运行一个命令
Alt + F4 关闭当前窗口
Alt + F6
Alt + F10 窗口最大化 / 还原
Ctrl + Alt + T 打开终端

Win + Space 切换输入法
Alt + Space 窗口菜单
Win + C 计算器 自定义
Win + F 文件浏览器 自定义

切换

Alt + Tab 切换窗口
Win + Tab 切换程序
Alt + ~ / Win + ~ 同一个程序的不通窗口切换

截图

屏幕截图并保存:PrntScr
Shift 选区截屏,加 Alt 窗口截屏,加 Ctrl 截屏到剪切板

PS:Ubuntu 22.04 之后,截图的逻辑调整了。
交互式截屏:PrntScr
Shift 屏幕截图并保存,同时复制到剪切板
Alt 窗口截屏并保存,同时复制到剪切板
Ctrl 没有任何效果

终端

Ctrl + Shift + N new window
Ctrl + Shift + Q quit window

Ctrl + Shift + T 打开新标签
Ctrl + Shift + W close tab

Ctrl + Shift + PageUp Tab 左移
Ctrl + Shift + PageDown Tab 右移

Ctrl + Shift + C 复制
Ctrl + Shift + V 粘贴

Ctrl + / 清空

其他

F9 打开 CopyQ 自定义

#68 Linux 磁盘使用情况检查

2020-03-14

我们一般使用 du 命令,不正是做这个用的么(Disk Usage)?

du -hsc          # 当前目录占用空间大小
du -hsc *.log*   # 查看文件大小
du -hsc a b c d  # 查看指定几个目录的大小

#67 Linux 相关标准

2020-03-09
  • Linux Standard Base (LSB)
  • Filesystem Hierarchy Standard (FHS)
  • Application Programming Interface (API) Standards
  • Single UNIX Specification version 4, The Open Group
  • Single UNIX Specification version 3, The Open Group
  • Single UNIX Specification version 2, The Open Group
  • DWARF Standards
  • DWARF Version 4
  • DWARF Version 3
  • DWARF Version 2.0
  • ELF and ABI Standards
  • Tool Interface Standard (TIS) Portable Formats Specification, version 1.1
  • Tool Interface Standard (TIS) Portable Formats Specification, version 1.2
  • System V ABI Edition 4.1
  • System V ABI - DRAFT 24 April 2001
  • Processor Specific ELF documents
  • System V Application Binary Interface Intel386 Architecture Processor Supplment, Fourth Edition
  • Intel Itanium Processor specific ABI
  • System V Application Binary Interface x86-64 Architecture Processor Supplement Draft Version 0.95 | v0.98 | v0.99
  • System V Application Binary Interface MIPS RISC Processor Supplment, 3rd Edition
  • System V Application Binary Interface PowerPC Processor Supplment. This is one of two known PPC ELF standards. This one was developed by SunSoft. The two versions are not identical, and applications can only conform to one or the other, while it seems possible for an implementation to support both simulaneously.
  • ARM Processor Supplment
  • Processor-Specific ELF Supplement for PA-RISC
  • Motorola 8 and 16 bit Embedded ABI
  • Application Binary Interface (ABI) Specifications/Standards
  • gLSB v1.2, Linux Standard Base
  • archLSB-IA32 v1.2, Linux Standard Base
  • archLSB-PPC32 v1.2, Linux Standard Base
  • 3DNow! (AMD K6 & Athlon), AMD
  • AMD Extensions to the 3DNow1 and MMX Instruction Set, AMD
  • Linux for S/390 (32bit) ELF ABI Supplement, IBM
  • Linux for zSeries (64bit) ELF ABI Supplement, IBM
  • C++ ABI for Itanium, v1.86
  • C++ ABI for Itanium, v1.83
  • C++ ABI for Itanium, v1.75
  • Intel Itanium Processor-Specific ABI
  • Related ABI Projects
  • Moblin 1.9.0 spec
  • Moblin 1.9.1 spec

参考资料与拓展阅读

#66 Shell 历史

2020-01-07

参考: https://en.wikipedia.org/wiki/Comparison_of_command_shells

Linux Shell

  • Thompson shell (sh), 1971, Unix v1 - v6
    只存在历史中
    在 Bourne shell 之前,贝尔实验室还出了一个 Mashey shell,只出现在 1976 年的 PWB UNIX 中(所以又叫 PWB shell),没有大范围使用
  • Bourne shell (sh), 1977, Unix v7
  • C shell (csh), 1978 img
  • TENEX C shell (tcsh), 1983 img
    是 TENEX 系统开发的 csh 兼容 Shell
  • Korn shell (ksh), 1983 img
  • Bash, 1989 img
  • Almquist shell (ash), 1989 img
  • zsh, 1990 img
  • Debian Almquist shell (dash), an ash fork, 1997
    主体随 ash 是 BSD 协议,不过,可能是为了保持对 bash 的兼容,采用了 bash 项目的一个文件
  • fish, 2005 img

分类

  • ksh, bash, zsh, ash, dash 都是 Bourne shell 兼容。
  • csh, tcsh 就是 csh 兼容。
  • fish

说明

  1. Unix 上的 sh 有版权问题, 所以 BSD 和 Linux 上的 /bin/sh 都指向某一种兼容 Shell (一般是默认 Shell)
  2. RHEL/CentOS 上,默认 Shell 是 Bash
  3. Debian/Ubuntu 上,默认 Shell 是 Dash
  4. FreeBSD 上默认采用 tcsh, 基于 FreeBSD 的 GhostBSD 采用 fish
  5. OpenBSD 上默认采用 pdksh (pd: Public Domain), 不知道和 tsh 什么关系。可能是 OpenBSD 维护的 tsh 兼容 Shell。
  6. NetBSD 上默认采用 ash
  7. 由于 ash 非常轻量级,BusyBox 采用了 dash fork
    然后 BusyBox 被 Alpine Linux, Tiny Core Linux 或者其他嵌入式 Linux (比如 OpenWrt) 采用,所以这些系统的默认 Shell 应该就是 ash

所以可能使用最广泛的 Shell 是 ash 和 bash。

其他:MacOS 上曾经默认采用 bash, 后来专向了 zsh

Windows 平台

  • command.com, 1980
  • cmd.exe, 1993
  • PowerShell, 2006