Filter

Filter 常用于从大量文本、数据中提取需求的部分。下面介绍几个常用的 filter 命令。

cut

$ cut -c 5-8 textfile.txt 	    # 切出 textfile.txt 中每行的第 5 到第 8 个 character
$ cut -f2-4 -d',' textfile.txt # 切出 textfile.txt 中每行的第 2 到第 4 块 field,field 由 delimiter “,” 确定
$ cut -f2,4 -d'|' textfile.txt # 切出 textfile.txt 中每行的第 2 、第 4 块 field, field 由 delimiter “|” 确定

uniq

$ sort | uniq -c	# aggregation (带 count),类似 SQL 中的 GROUPBY

egrep

$ egrep -o <pattern> textfile.txt	# 只返回 match pattern 的部分
$ egrep -x <pattern> textfile.txt # 只有文件的某行 (line) fully match pattern 的时候才返回
$ egrep -w <pattern> textfile.txt # 只有文件的某字 (word) fully match pattern 的时候才返回

wc

$ wc -l textfile.txt		# 返回行数计数
$ wc -m textfile.txt # 返回char计数
$ wc -w textfile.txt # 返回word计数

tr

$ tr  <srcChars> <destChars>	# 将输入字符中含有 srcChars 的字符对应替换成 destChars
$ tr 'a-z' 'A-Z' # 将输入字符的小写字母转为大写
$ tr -d ' ' # 删除输入字符中所有的空格
$ tr -cs 'a-zA-Z0-9' '\n' # 切出在 SET 'a-zA-Z0-9' 中的连续字符,并按行输出

sed

$ sed 's/regex/replacement/ig'	# 将 match regex 的部分全部替换成 replacement

cat

$ cat >new_textfile.txt<<eof	# 新建并从 stdin 按行写入文件 new_textfile.txt,直到遇到 eof 结束
$ cat >>new_textfile.txt<<eof # 从 stdin 按行连接写入文件 new_textfile.txt,直到遇到 eof 结束

Shell Script 入门

介绍 Shell 的一些常识和基本用法。

准备工作

Shell 脚本文件头

#!/bin/bash		# 告诉 Linux 该文件用 bash 运行

可执行模式

$ chmod +x shell_script.sh	# 修改 shell_script.sh 为可执行文件

常用命令

SSH

远程连接服务器(以 AWS 为例)

如果要连接 AWS, 需要联系管理员拿到私钥文件(扩展名为 .pem)。

$ ssh -i "private-key.pem" user_name@domain_name

如果想在 AWS 上运行 jupyter,tensorboard 等任务,可以使用 local forwarding 功能,将本地操作转发到远端服务器:

$ ssh -i "private-key.pem" <local-port>:127.0.0.1:<remote-port> user_name@domain_name

STDIN

$ read a_str_variable		# 从 stdin 读取字符串存入 a_str_variable 变量
$ read -t 3 a_str_variable # 3s 内无输入则退出(返回值 142)
$ read -s a_str_variable # 不显示输入字符,输入密码等机密信息时使用
$ read -p "Name? " # 相当于 Python 中的 input("Name? ")
$ read -r a_str_variable # 当输入的字符中含 “\” (反斜杠)时,保留 “\”。如果没有设置 -r,则反斜杠都会被 “吃掉”
# 参考:https://unix.stackexchange.com/questions/18886/why-is-while-ifs-read-used-so-often-instead-of-ifs-while-read/18936#18936

STDOUT

$ echo string

图片转换

$ convert original_image.png converted_image.jpg	# 图片格式转换,支持格式:jpg, png, gif, bmp, tif
$ convert -gravity south \ # 设置 draw text 的位置
-pointsize 36 \ # 设置字体大小
-draw "text 0,10 'Hello world'" original_image.jpg converted.jpg

文件搜索

$ find 							# 递归输出当前目录及其下所有文件或子目录,相当于`find . -print`
$ find dir # 递归输出dir 目录及其下所有文件或子目录
$ find dir -type f # 递归输出dir 目录下的所有文件
$ find dir -type d # 递归输出dir 目录及其下的所有子目录
$ find dir -name '*s2_COMP9041' # 递归输出dir 目录下所有符合通配符 `*s2_COMP9041` 的文件名或目录名(*此处不匹配斜杠/)
$ find dir -iname '*s2_comp9041'# 递归输出dir 目录下所有符合通配符 `*s2_comp9041` 的文件名或目录名(*此处不匹配斜杠/)(case insensitive)
$ find dir -path '*.sh' # 递归输出dir 目录下所有符合通配符 `*.sh` 的文件路径或目录路径(*此处匹配斜杠/)
$ find dir -ipath '*.SH' # 递归输出dir 目录下所有符合通配符 `*.sh` 的文件路径或目录路径(*此处匹配斜杠/)(case insensitive)
$ find dir -path '*.sh' \ # <或>操作
-or -path '*.pl' \
-or -path '*.py'
$ find dir -not -name '*.sh' \ # <非>操作
-type f
$ find dir -path '*.tmp' \ # <与>操作
-type f -delete # 递归删除dir 目录下所有路径符合通配符 `*.tmp` 的文件
$ find dir -name '*s2_COMP9041'\# 递归遍历dir 目录下所有
-type d \ # 的目录名
-prune # 如果目录名符合通配符 `'*s2_COMP9041'`,该目录下所有内容都被忽略
-print # 并输出(该选项可省略)
$ find dir -name '*s2_COMP9041'\# 递归遍历dir 目录下所有
-type d \ # 的目录名
-prune # 如果目录名符合通配符 `'*s2_COMP9041'`,该目录下所有内容都被忽略
-or -print # 输出其它未被忽略的目录及文件 # 有时可能仅仅需要排除目录,而非连同该目录下的所有文件。此时需要用到 -depth 选项
$ find dir -depth \ # 规定遍历顺序:遇到目录时,先列出目录中的文件,再列出目录自己
-name '*s2_COMP9041'\# 递归遍历dir 目录下所有
-type d \ # 的目录名
-prune # 如果目录名符合通配符 `'*s2_COMP9041'`,该目录下所有内容都被忽略
-or -print # 输出其它未被忽略的目录及文件
# 参考:https://math2001.github.io/post/bashs-find-command/

条件判断

#!/bin/bash

# 参数个数检查
if test $# -ne 2
# 或 if [ $# -ne 2 ]
# 或 if (( $# != 2 ))
then
echo Usage: "$0": '<non-negtive_int> <string>'
exit 1
fi # 参数类型检查
if test $1 -ge 0
# 或 if [ $1 -ge 0 ]
then
:
else
echo "$0": argument 1 must be a non-negtive integer!
exit 2
fi # 程序逻辑开始...

for Loop

# 遍历当前目录中的所有文件名
## Version 1 ## 不包括隐藏文件 ##
$ shopt -u dotglob
$ for filename in *
> do echo $filename
> done ## Version 2 ## 包括隐藏文件 ##
$ shopt -s dotglob
$ for filename in *
> do echo $filename
> done
## 根据 argument 输入的目录路径 `important_files/*` 遍历其中(包括子目录)所有的文件
# Input: $ readfile.sh important_files/*
$ for paths in "$@" # 注意! 1.不是$1 2.Add double quotes
> do
> find $paths -type f -print0 |
> while read -d $'\0' path
> do echo $path
> done
> done
# 类似 C语言 的用法
$ begin=0; end=5; step=1
$ for ((i=$begin; ((i < $end)); ((i += $step)) ))
> do echo $i
> done
# write files from arguments by line with line number
line_no=0
for word in "$@"
do
((line_no+=1))
echo $line_no $word
done > filepath

while Loop

# 类似 C语言 的用法
$ begin=0; end=5; step=1
$ i=$begin
$ while test $i -lt $end <OR> while [ $i -lt $end ] <OR> while (( i < end ))
> do echo $i
> (( i+=step ))
> done
# read files by line
while read line
do
echo $line
done < filepath

sh

$ sh -x your_shell_script.sh	# 用于追踪程序流,debug 时很实用

一些细节

Silence

$ egrep 'regex' filename.txt >/dev/null 2>&1	# >/dev/null —— stdout(1) 被扔到 /dev/null
# >2>&1 —— stderr(2) 跟着 stdout(1) 走
# 总结:不在屏幕上打印 stdout 和 stderr

引号问题,date 用法,图片处理

# 实际应用:给图片打tag
$ date_time=`date "+%H:%M %d %b,%Y"`
$ convert -draw "text 0,0 '$date_time'" oringal.jpg tagged.jpg

Linux 文件名相关问题

文件名以 “-” 开头

$ mv -- filename(以‘-’开头) newfilename		# -- 表示下一个参数不会是 Option(必须是文件名)

文件名中绝对不能包含的字符

['/', '\0']

文件名大小写敏感

$ touch important_file.db Important_File.Db IMPORTANT_FILE.DB
$ ls
important_file.db Important_File.Db IMPORTANT_FILE.DB

Shell 常用命令、基本用法总结的更多相关文章

  1. shell常用命令的用法

    1. 如何把 /etc/passwd 中用户uid 大于500 的行给打印出来?awk -F ':' '$3>500' /etc/passwd 2. awk中 NR,NF两个变量表示什么含义?N ...

  2. Shell 常用命令总结

      Shell常用命令总结 1  ls命令:列出文件 ls -la 列出当前目录下的所有文件和文件夹 ls a* 列出当前目录下所有以a字母开头的文件 ls -l *.txt 列出当前目录下所有后缀名 ...

  3. (转)Hbase shell 常用命令(1)

    Hbase shell 常用命令(1) link:http://blog.csdn.net/scutshuxue/article/details/6988348 下面我们看看HBase Shell的一 ...

  4. hbase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  5. 【转载】HBase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  6. (三)Linux Shell编程——Shell常用命令(输出、判断、循环、函数、包含)

    3. 常用命令 3.1 输出 3.1.1 echo命令 echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串.命令格式: echo arg name="coding" ...

  7. RedHat Linux Shell常用命令(多数也适用于Unix和AIX)

    注:本文转载自疯狂的矩阵一文,http://www.cnblogs.com/520sojustdoit/p/4642568.html --------------------------------- ...

  8. Linux / mac OS Shell常用命令

    一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示文件属性,包括大小,日期,符号连接,是否可 ...

  9. Linux/Mac/Shell常用命令

    常用命令 · ls 查看当前目录下的文件 · cd 进入某目录 · cd - 跳转回前一目录 · cd ~ 进入当前用户个人目录 · pwd 输出当前所在路径 · mkdir 新建文件夹 · touc ...

随机推荐

  1. 用HTML编写迪士尼乐园页面

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head lang="e ...

  2. Cocopods Search失败的坑

    最近看了下如何使用cocopods来制作自己的公有库,然后果断的按照教程做了一遍,然后提交审核.完成之后意外的发现使用pod search xxx的时候报了一大堆的日志出来,pod的其他功能可以使用, ...

  3. 常用EL函数汇总 fn:contains ,fn:substring,fn:substringAfter...

    由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用.这些EL函数在JSTL开发包中进行描述,因此在JSP页面中使用SUN公司的E ...

  4. Elasticsearch 索引操作

    一.创建 语法: PUT /索引库名称 { "settings": { "number_of_shards": 分片数量, "number_of_re ...

  5. I2C驱动

    在I2C总线驱动下,也是硬件设备和驱动分离,使以就需要通过它们的名字来匹配,这样驱动的probe函数才能被调用 查看linux内核的Documents目录下的说明文件,可知构造i2c设备有4种方法: ...

  6. Python学习 :函数

    函数 函数(Functions) 是指可重复使用的程序片段.它们允许你为某个代码块赋予名字,允许你通过这一特殊的名字在你的程序任何地方来运行代码块,并可重复任何次数.这就是调用(Calling)函数. ...

  7. [Cracking the Coding Interview] 4.1 Route Between Nodes 节点间的路径

    Given a directed graph, design an algorithm to find out whether there is a route between nodes. 这道题让 ...

  8. 【BZOJ4818】序列计数(动态规划,生成函数)

    [BZOJ4818]序列计数(生成函数) 题面 BZOJ 题解 显然是求一个多项式的若干次方,并且是循环卷积 或者说他是一个\(dp\)也没有问题 发现项数很少,直接暴力乘就行了(\(FFT\)可能还 ...

  9. 优步UBER司机全国各地奖励政策汇总 (2月15日-2月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 成都Uber优步司机奖励政策(1月11日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...