数据文件处理——更多的bash shell命令(二)

一、排序数据

####### 1、sort - sort lines of text files

  • man手册中关于sort的说明:
    • sort -t ‘分隔符’ -k pos1 [,pos2] 按照以分隔符划分的第pos1段来比较排序
    • sort只是一种排序的显示,并没有真正改变文件的内容
SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F
DESCRIPTION
       Write sorted concatenation of all FILE(s) to standard output.
       With no FILE, or when FILE is -, read standard input.
       Mandatory arguments to long options are mandatory for short options too.  Ordering options:
# *** == 排列时,忽略开始的空白 == ***
       -b, --ignore-leading-blanks
              ignore leading blanks
# 只考虑空白,数字和字母的排序
       -d, --dictionary-order
              consider only blanks and alphanumeric characters
# 忽略大小写,但一般情况大写在前面如aBb
       -f, --ignore-case
              fold lower case to upper case characters
# 使数字按大小排序,而不是拆分成字符来排序
       -g, --general-numeric-sort
              compare according to general numerical value
# 排序时,忽略不可打印字符
       -i, --ignore-nonprinting
              consider only printable characters
# 按月份大小排序
       -M, --month-sort
              compare (unknown) < 'JAN' < ... < 'DEC'
# 按照可读数值大写排序,识别K G等
       -h, --human-numeric-sort
              compare human readable numbers (e.g., 2K 1G)
# 按字符串比较大小的方式来排序
       -n, --numeric-sort
              compare according to string numerical value
# 按随机生产的散列表来排序
       -R, --random-sort
              shuffle, but group identical keys.  See shuf(1)

       --random-source=FILE
              get random bytes from FILE
# 反向排序
       -r, --reverse
              reverse the result of comparisons

       --sort=WORD
              sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

       -V, --version-sort
              natural sort of (version) numbers within text
               Other options:

       --batch-size=NMERGE
              merge at most NMERGE inputs at once; for more use temp files
# 不排序,报告第一个无序行
       -c, --check, --check=diagnose-first
              check for sorted input; do not sort
# 不排序,也不报告
       -C, --check=quiet, --check=silent
              like -c, but do not report first bad line

       --compress-program=PROG
              compress temporaries with PROG; decompress them with PROG -d

       --debug
              annotate the part of the line used to sort, and warn about questionable usage to stderr

       --files0-from=F
              read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

       -k, --key=KEYDEF
              sort via a key; KEYDEF gives location and type

       -m, --merge
              merge already sorted files; do not sort

       -o, --output=FILE
              write result to FILE instead of standard output

       -s, --stable
              stabilize sort by disabling last-resort comparison

       -S, --buffer-size=SIZE
              use SIZE for main memory buffer

       -t, --field-separator=SEP
              use SEP instead of non-blank to blank transition

       -T, --temporary-directory=DIR
              use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories

 Other options:

       --batch-size=NMERGE
              merge at most NMERGE inputs at once; for more use temp files

       -c, --check, --check=diagnose-first
              check for sorted input; do not sort

       -C, --check=quiet, --check=silent
              like -c, but do not report first bad line

       --compress-program=PROG
              compress temporaries with PROG; decompress them with PROG -d

       --debug
              annotate the part of the line used to sort, and warn about questionable usage to stderr

       --files0-from=F
              read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

       -k, --key=KEYDEF
              sort via a key; KEYDEF gives location and type

       -m, --merge
              merge already sorted files; do not sort

       -o, --output=FILE
              write result to FILE instead of standard output

       -s, --stable
              stabilize sort by disabling last-resort comparison

       -S, --buffer-size=SIZE
              use SIZE for main memory buffer

       -t, --field-separator=SEP
              use SEP instead of non-blank to blank transition

       -T, --temporary-directory=DIR
              use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories

二、搜索数据 grep
1、grep [option] pattern [file]
  • 其中:
    • pattern使用正则表达式POSIX的BRE模式
    • 常用的选项有:
      • -v反向查找,查找不匹配项
      • -n显示行号
      • -c只显示匹配的行号
      • -e 可以连续使用-e来匹配多个pattern。
三、压缩数据和归档数据
1、压缩数据
工具文件扩展名描述
bzip2.bz2采用Burrows-Wheeler块排序文本压缩算法和霍夫曼编码
compress.Z最初的Unix文件压缩工具,已经快没人用了
gzip.gzGNU压缩工具,用Lempel-Ziv编码
zip.zipWindows上PKZIP工具的Unix实现
  • gzip 用来压缩文件
  • gzcat 用来查看压缩过的文件的neirong
  • gunzip 用来解压文件
  • gzip 是通过通配符来定位要压缩的文件的
2、归档数据
tar function [options] object1 object2 ...`
  • tar命令的功能(function选项)
功能长名称描述
-A–concatenate将一个已有tar归档文件追加到另一个已有tar归档文件
-c–create创建一个新的tar归档文件
-d–diff检查归档文件和文件系统的不同之处
-–delete从已有的归档文件中删除
-r–append追加文件到已有的tar归档文件末尾
-t–list列出已有归档文件的内容
-u–update将比tar归档文件中已有的同名文件新的文件追加到该tar归档文件中
-x–extract从已有的归档文件中提取文件
  • tar命令选项
选项描述
-C dir切换到指定目录
-f file将结果输出到文件或设备file
-j将输出重定向给bzip2命令来压缩内容
-p保留所有文件权限
-v在处理文件时显示文件
-z将输出重定向给gzip命令来压缩内容
  • tar -xjvf sysconfig.tar.bz2命令来解压.tar.bz2文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值