关于grep:学完14个实例掌握-grep-命令的使用方法

Grep是Linux/Unix零碎中的一个命令行工具,用于从文件中搜寻文本或字符串。Grep代表全局正则表达式打印。当咱们应用指定字符串运行grep命令时,如果匹配,则它将显示蕴含该字符串的文件行,而不批改现有文件的内容。 命令语法$ grep <Options> <Search String> <File-Name>命令选项: (1) 在文件中搜寻单词或字符串当咱们运行grep命令,后跟搜寻字符串或模式时,它将打印文件的匹配行。示例如下所示。 例如:在文件 /etc/passwd 文件中搜寻 nobody $ grep nobody /etc/passwdnobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin$(2) 多文件中的搜寻模式能够应用grep命令在多个文件中搜寻单词或模式。 例如:在 /etc/passwd,/etc/shadow, /etc/gshadow文件中搜寻 linuxtechi $ sudo grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow (3) 打印与模式匹配的文件名假如咱们想列出蕴含单词“root”的文件名,能够在grep命令中应用“-l”选项,后跟单词(模式)和文件。 $ grep -l 'root' /etc/fstab /etc/passwd /etc/mtab/etc/passwd$(4) 显示带有行号的输入行应用 “-n” 选项显示与模式或单词匹配的行及其编号。 $ grep -n 'nobody' /etc/passwd18:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin$(5) 反转模式匹配应用grep命令中的选项 “-v”,咱们能够显示与模式不匹配的行 $ grep -v 'nobody' /etc/passwd 6) 打印以特定字符结尾的所有行Bash Shell 将插入符号 “^” 视为特殊字符,用于标记行或单词的结尾。 让咱们显示文件 /etc/passwd 中以 “backup” 结尾的行 $ grep ^backup /etc/passwdbackup:x:34:34:backup:/var/backups:/usr/sbin/nologin$(7) 打印以特定字符结尾的所有行Bash Shell 将美元符号 “$” 视为一个特殊字符,用于标记行或字的结尾。 ...

August 31, 2022 · 2 min · jiezi

关于grep:LinuxGrep-常用参数

依照最常见到意识的水平,顺次排序介绍 * -E ==等价于egrep,extension扩大正则* -v ==等价于^,取反* -o ==等价于output,将抓取的后果输入* -c ==等价于wc-l, count取数* -i ==等价于ignore,疏忽大小写* -w ==等价于Vlookup,第四个参数False,0(准确匹配)* -n ==等价于sed "="或者 cat -n, 为显示的行增加行号* -A ==等价于After, 自身内容加之后的N行* -B ==等价于Before,自身内容之前的N行-E -v 两头空进去的中央,正是下面显示的两行数据 如果换成大写的V,则会报错如下,版本问题: -c等价于管道命令加 wc -l -n 为显示的行增加行号 cat -n sed "=" grep.txt | sed 'N;s#\n# #' -w准确匹配,如果只是简略需要匹配一个day的单词。然而文件内容不仅蕴含day,还有Monday,Tuesday,Wednesday等等。如果不准确匹配的话,就会将所有单词中蕴含day的抓取进去 准确匹配前准确匹配后 -i Ignore 疏忽每个字母大小写 -A 取有数字前面的2行元文件的所有内容如下: 将数据前面的2行取出来的后果如下: -B 取有数字后面的2行后果如下:

September 13, 2021 · 1 min · jiezi

运行shell脚本时进程数量变多

写了一个很简单的脚本,用于统计memcache进程的数量: #!/bin/bashecho `ps aux | grep memcache | grep -v grep | wc -l`然而在执行时却遇到了问题: [work@ oss_memcache_status]$ pwd/home/work/cdn/monitor/ocelot-scripts/oss_memcache_status[work@ oss_memcache_status]$ ./run.sh1[work@ oss_memcache_status]$ ../oss_memcache_status/run.sh3这个原因是因为我们在执行shell脚本时,会通过子进程的方式来执行,因此统计数量比预期要多。解决方案为grep -v bash。 执行shell脚本的方式我们有三种常用的方式执行shell脚本: source run.sh: 会在当前进程下执行脚本,执行时的变量会保存下来。. run.sh: 和source方法基本一样,区别在于source不是POSIX要求的。./run.sh: 如果脚本以#!/bin/bash开头,会在单独的子进程中执行,执行完毕后变量不保存。否则和source一样。因此,在上面的脚本中,我们在执行时,因为是以#!/bin/bash开头,会在子进程中执行,我们改动一下脚本看都是哪些进程: #!/bin/bashecho `ps aux | grep memcache | grep -v grep`执行: work 24414 0.0 0.0 108116 1276 pts/0 S+ 15:31 0:00 /bin/bash ../oss_memcache_status/run.shwork 24415 0.0 0.0 108116 612 pts/0 S+ 15:31 0:00 /bin/bash ../oss_memcache_status/run.shwork 30558 0.0 0.0 371236 47096 ? Ssl 2016 15:14 /usr/local/bin/memcached -d -m 256 -u nobody -l localhost -p 11211我们通过结果,可以看出来,第一个进程和第二个进程的父进程相同,他们都属于当前终端启动的进程。前两个分别为执行run脚本的进程和调起的子进程(这两个什么区别,我也不太清楚),第三个为真正的进程。 ...

April 25, 2019 · 1 min · jiezi

vim 的 grep 插件`Leaderf rg`:grep 和模糊匹配的完美结合

前言vim有很多著名的grep插件,我使用过的有ack.vim,ag.vim和ctrlsf.vim,它们应该也是目前用户最多的几个了。ack.vim起步比较早,早期后端grep工具是ack,后来也支持ag(the_silver_searcher),pt(the_platinum_searcher),rg(ripgrep)等工具了。它是一个比较传统的grep插件,不支持异步,要等到grep结束后才能显示结果,在大的项目中grep会卡好一阵子。它貌似也不再维护,我N久前提交的pull request还挂在那,它最近的代码更新在11个月前。ag.vim它其实是抄袭ack.vim,没错,是抄袭。在早期ack.vim还不支持ag时,它的作者在ack.vim代码的基础上稍微改了改,支持了ag。后来被ack.vim的作者给怼了,就放弃了对ag.vim的维护。目前功能上小于ack.vim。ctrlsf.vim这是国人开发的一个插件, 后端grep工具支持ack/ag/pt/rg,同时也支持异步,不过需要Vim 8.0.1039+或者NeoVim才支持异步。这个插件很好用,在我开发Leaderf rg之前一直使用的是它。Leaderf rg顾名思义,后端基于rg,由于是LeaderF的子功能,基因上就决定它完美支持异步。同时LeaderF又是一个非著名的模糊查找插件,这使它可以在grep结果的基础上再通过模糊匹配的方式进行二次过滤,来帮助用户更快地锁定目标,这是目前上面提到的插件所不具备的。为什么选择rg(ripgrep)快速grep工具目前有ag, rg, pt, sift, ucg等。 我选择rg有以下几点原因:速度比较快,rg的README上有作者的对比,我实测也是rg快点。Windows上bug少(bug到目前还没发现),ag和pt都遇到过bug。作者很活跃,提的issue能很快得到回复。rg功能相对多些,可以从rg –help 看出来。Leaderf rg 使用介绍Leaderf rg的使用也比较简单,只要Leaderf[!] + rg命令和选项(同命令行上一样)就可以了。具体使用方法可以用:Leaderf rg -h来查看。usage: Leaderf[!] rg [-h] [-e <PATTERN>…] [-F] [-i] [-L] [-P] [-S] [-s] [-v] [-w] [-x] [–hidden] [–no-config] [–no-ignore] [–no-ignore-global] [–no-ignore-parent] [–no-ignore-vcs] [–no-pcre2-unicode] [-E <ENCODING>] [-M <NUM>] [-m <NUM>] [–max-depth <NUM>] [–max-filesize <NUM+SUFFIX?>] [–path-separator <SEPARATOR>] [–sort <SORTBY>] [–sortr <SORTBY>] [-f <PATTERNFILE>…] [-g <GLOB>…] [–iglob <GLOB>…] [–ignore-file <PATH>…] [–type-add <TYPE_SPEC>…] [-t <TYPE>…] [-T <TYPE>…] [–current-buffer | –all-buffers] [–recall] [–append] [–reverse] [–stayOpen] [–input <INPUT> | –cword] [–top | –bottom | –left | –right | –belowright | –aboveleft | –fullScreen] [–nameOnly | –fullPath | –fuzzy | –regexMode] [–nowrap] [<PATH> [<PATH> …]]optional arguments: -h, –help show this help message and exitspecific arguments: -e <PATTERN>…, –regexp <PATTERN>… A pattern to search for. This option can be provided multiple times, where all patterns given are searched. -F, –fixed-strings Treat the pattern as a literal string instead of a regular expression. -i, –ignore-case Searches case insensitively. -L, –follow Follow symbolic links while traversing directories. -P, –pcre2 When this flag is present, rg will use the PCRE2 regex engine instead of its default regex engine. -S, –smart-case Searches case insensitively if the pattern is all lowercase, case sensitively otherwise. -s, –case-sensitive Searches case sensitively. -v, –invert-match Invert matching. Show lines that do not match the given patterns. -w, –word-regexp Only show matches surrounded by word boundaries. This is roughly equivalent to putting \b before and after all of the search patterns. -x, –line-regexp Only show matches surrounded by line boundaries. –hidden Search hidden files and directories. By default, hidden files and directories are skipped. –no-config Never read configuration files. When this flag is present, rg will not respect the RIPGREP_CONFIG_PATH environment variable. –no-ignore Don’t respect ignore files (.gitignore, .ignore, etc.). This implies –no-ignore-parent and –no-ignore-vcs. –no-ignore-global Don’t respect ignore files that come from ‘global’ sources such as git’s core.excludesFile configuration option (which defaults to $HOME/.config/git/ignore). –no-ignore-parent Don’t respect ignore files (.gitignore, .ignore, etc.) in parent directories. –no-ignore-vcs Don’t respect version control ignore files (.gitignore, etc.). –no-pcre2-unicode When PCRE2 matching is enabled, this flag will disable Unicode mode, which is otherwise enabled by default. -E <ENCODING>, –encoding <ENCODING> Specify the text encoding that rg will use on all files searched. -M <NUM>, –max-columns <NUM> Don’t print lines longer than this limit in bytes. -m <NUM>, –max-count <NUM> Limit the number of matching lines per file searched to NUM. –max-depth <NUM> Limit the depth of directory traversal to NUM levels beyond the paths given. –max-filesize <NUM+SUFFIX?> Ignore files larger than NUM in size. This does not apply to directories. –path-separator <SEPARATOR> Set the path separator to use when printing file paths. –sort <SORTBY> This flag enables sorting of results in ascending order. –sortr <SORTBY> This flag enables sorting of results in descending order. -f <PATTERNFILE>…, –file <PATTERNFILE>… Search for patterns from the given file, with one pattern per line. (This option can be provided multiple times.) -g <GLOB>…, –glob <GLOB>… Include or exclude files and directories for searching that match the given glob.(This option can be provided multiple times.) –iglob <GLOB>… Include or exclude files and directories for searching that match the given glob. Globs are matched case insensitively.(This option can be provided multiple times.) –ignore-file <PATH>… Specifies a path to one or more .gitignore format rules files. –type-add <TYPE_SPEC>… Add a new glob for a particular file type. -t <TYPE>…, –type <TYPE>… Only search files matching TYPE. Multiple type flags may be provided. -T <TYPE>…, –type-not <TYPE>… Do not search files matching TYPE. Multiple type-not flags may be provided. <PATH> A file or directory to search. Directories are searched recursively. Paths specified on the command line override glob and ignore rules. –current-buffer Searches in current buffer. –all-buffers Searches in all listed buffers. –recall Recall last search. If the result window is closed, reopen it. –append Append to the previous search results.common arguments: –reverse show results in bottom-up order –stayOpen don’t quit LeaderF after accepting an entry –input <INPUT> specifies INPUT as the pattern inputted in advance –cword current word under cursor is inputted in advance –top the LeaderF window is at the top of the screen –bottom the LeaderF window is at the bottom of the screen –left the LeaderF window is at the left of the screen –right the LeaderF window is at the right of the screen –belowright the LeaderF window is at the belowright of the screen –aboveleft the LeaderF window is at the aboveleft of the screen –fullScreen the LeaderF window takes up the full screen –nameOnly LeaderF is in NameOnly mode by default –fullPath LeaderF is in FullPath mode by default –fuzzy LeaderF is in Fuzzy mode by default –regexMode LeaderF is in Regex mode by default –nowrap long lines in the LeaderF window won’t wrapIf [!] is given, enter normal mode directly.注意:如果:Leaderf后面有感叹号,会直接进入normal模式;如果没有感叹号,则是输入模式,此时可以输入字符来进行模糊匹配过滤。可以用tab键在两个模式间来回切换。Leaderf rg基本支持rg所有的必要选项,用户如果对rg命令比较熟悉,可以在vim命令行内输入:Leaderf, 然后手敲rg命令,命令选项还可以通过tab来补全。当然,更聪明的做法是定义一些快捷键。例如:" search word under cursor, the pattern is treated as regex, and enter normal mode directlynoremap <C-F> :<C-U><C-R>=printf(“Leaderf! rg -e %s “, expand("<cword>”))<CR>” search word under cursor, the pattern is treated as regex," append the result to previous search results.noremap <C-G> :<C-U><C-R>=printf(“Leaderf! rg –append -e %s “, expand("<cword>”))<CR>” search word under cursor literally only in current buffernoremap <C-B> :<C-U><C-R>=printf(“Leaderf! rg -F –current-buffer -e %s “, expand("<cword>”))<CR>” search word under cursor literally in all listed buffersnoremap <C-D> :<C-U><C-R>=printf(“Leaderf! rg -F –all-buffers -e %s “, expand("<cword>”))<CR>” search visually selected text literally, don’t quit LeaderF after accepting an entryxnoremap gf :<C-U><C-R>=printf(“Leaderf! rg -F –stayOpen -e %s “, leaderf#Rg#visual())<CR>” recall last search. If the result window is closed, reopen it.noremap go :<C-U>Leaderf! rg –recall<CR>” search word under cursor in *.h and *.cpp files.noremap <Leader>a :<C-U><C-R>=printf(“Leaderf! rg -e %s -g *.h -g *.cpp”, expand("<cword>"))<CR>" the same as abovenoremap <Leader>a :<C-U><C-R>=printf(“Leaderf! rg -e %s -g *.{h,cpp}”, expand("<cword>"))<CR>" search word under cursor in cpp and java files.noremap <Leader>b :<C-U><C-R>=printf(“Leaderf! rg -e %s -t cpp -t java”, expand("<cword>"))<CR>" search word under cursor in cpp files, exclude the .hpp filesnoremap <Leader>c :<C-U><C-R>=printf(“Leaderf! rg -e %s -t cpp -g !.hpp”, expand("<cword>"))<CR>参考:rg的glob语法。顺便说一下,直接在vim命令行敲:Leaderf rg,就会有传说中的"grep on the fly"的功能哦,同时支持fuzzy和regex两种模式。会不会支持ag等其他grep工具不会。 首先,ripgrep已经足够强大,基本不存在别的工具有而ripgrep没有的功能。其次,ripgrep有编译好的Windows、Linux和MacOS上的binary,可以在这些平台上很容易安装。再者,由于作者比较懒,不想再实现重复的功能。 ...

January 15, 2019 · 5 min · jiezi

日志分析必备指令集【来自一段线上日志的查看的经历】

线上日志查看基础查看线上机器的一些信息和基础命令:du、df查看大小相关cat、zcat、less、tail、head查看文件内容grep、awk处理文件内容sort、uniq、wc统计scp文件传输du、df查看大小相关df 查看系统挂载磁盘大小df [选项]… [FILE]…文件-a, –all 包含所有的具有 0 Blocks 的文件系统文件–block-size={SIZE} 使用 {SIZE} 大小的 Blocks文件-h, –human-readable 使用人类可读的格式(预设值是不加这个选项的…)文件-H, –si 很像 -h, 但是用 1000 为单位而不是用 1024文件-i, –inodes 列出 inode 资讯,不列出已使用 block文件-k, –kilobytes 就像是 –block-size=1024文件-l, –local 限制列出的文件结构文件-m, –megabytes 就像 –block-size=1048576文件–no-sync 取得资讯前不 sync (预设值)文件-P, –portability 使用 POSIX 输出格式文件–sync 在取得资讯前 sync文件-t, –type=TYPE 限制列出文件系统的 TYPE文件-T, –print-type 显示文件系统的形式文件-x, –exclude-type=TYPE 限制列出文件系统不要显示 TYPE文件-v (忽略)常见使用实例:df -hdu会显示指定的目录或文件所占用的磁盘空间du [-abcDhHklmsSx][-L <符号连接>][-X <文件>][–block-size][–exclude=<目录或文件>][–max-depth=<目录层数>][–help][–version][目录或文件]参数说明:-a或-all 显示目录中个别文件的大小。-b或-bytes 显示目录或文件大小时,以byte为单位。-c或–total 除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和。-D或–dereference-args 显示指定符号连接的源文件大小。-h或–human-readable 以K,M,G为单位,提高信息的可读性。-H或–si 与-h参数相同,但是K,M,G是以1000为换算单位。-k或–kilobytes 以1024 bytes为单位。-l或–count-links 重复计算硬件连接的文件。-L<符号连接>或–dereference<符号连接> 显示选项中所指定符号连接的源文件大小。-m或–megabytes 以1MB为单位。-s或–summarize 仅显示总计。-S或–separate-dirs 显示个别目录的大小时,并不含其子目录的大小。-x或–one-file-xystem 以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过。-X<文件>或–exclude-from=<文件> 在<文件>指定目录或文件。–exclude=<目录或文件> 略过指定的目录或文件。–max-depth=<目录层数> 超过指定层数的目录后,予以忽略。使用实例:查看当前文件夹的一级内容大小du -h –max-depth=1cat、zcat、less、tail、head查看文件内容 cat 命令用于连接文件并打印到标准输出设备上cat [-AbeEnstTuv] [–help] [–version] fileName-n 或 --number:由 1 开始对所有输出的行数编号。-b 或 --number-nonblank:和 -n 相似,只不过对于空白行不编号。-s 或 --squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。-v 或 --show-nonprinting:使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外。-E 或 --show-ends : 在每行结束处显示 $。-T 或 --show-tabs: 将 TAB 字符显示为 ^I。-A, --show-all:等价于 -vET。-e:等价于"-vE"选项;-t:等价于"-vT"选项;zcatt命令用于不真正解压缩文件,就能显示压缩包中文件的内容的场合。head显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行head [参数]… [文件]… -q 隐藏文件名-v 显示文件名-c&lt;字节&gt; 显示字节数-n&lt;行数&gt; 显示的行数tail用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件tail[必要参数][选择参数][文件] -f 循环读取-q 不显示处理信息-v 显示详细的处理信息-c&lt;数目&gt; 显示的字节数-n&lt;行数&gt; 显示行数--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束. -q, --quiet, --silent 从不输出给出文件名的首部 -s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒 less可以随意浏览文件less [参数] 文件-b &lt;缓冲区大小&gt; 设置缓冲区的大小-e 当文件显示结束后,自动离开-f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件-g 只标志最后搜索的关键词-i 忽略搜索时的大小写-m 显示类似more命令的百分比-N 显示每行的行号-o &lt;文件名&gt; 将less 输出的内容在指定文件中保存起来-Q 不使用警告音-s 显示连续空行为一行-S 行过长时间将超出部分舍弃-x &lt;数字&gt; 将“tab”键显示为规定的数字空格/字符串:向下搜索“字符串”的功能?字符串:向上搜索“字符串”的功能n:重复前一个搜索(与 / 或 ? 有关)N:反向重复前一个搜索(与 / 或 ? 有关)b 向后翻一页d 向后翻半页h 显示帮助界面Q 退出less 命令u 向前滚动半页y 向前滚动一行空格键 滚动一行回车键 滚动一页[pagedown]: 向下翻动一页[pageup]: 向上翻动一页grep、awk处理文件内容grep用于查找文件里符合条件的字符串。grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][–help][范本样式][文件或目录…]-a 或 –text : 不要忽略二进制的数据。-A<显示行数> 或 –after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。-b 或 –byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。-B<显示行数> 或 –before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。-c 或 –count : 计算符合样式的列数。-C<显示行数> 或 –context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。-d <动作> 或 –directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。-e<范本样式> 或 –regexp=<范本样式> : 指定字符串做为查找文件内容的样式。-E 或 –extended-regexp : 将样式为延伸的普通表示法来使用。-f<规则文件> 或 –file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。-F 或 –fixed-regexp : 将样式视为固定字符串的列表。-G 或 –basic-regexp : 将样式视为普通的表示法来使用。-h 或 –no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。-H 或 –with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。-i 或 –ignore-case : 忽略字符大小写的差别。-l 或 –file-with-matches : 列出文件内容符合指定的样式的文件名称。-L 或 –files-without-match : 列出文件内容不符合指定的样式的文件名称。-n 或 –line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。-q 或 –quiet或–silent : 不显示任何信息。-r 或 –recursive : 此参数的效果和指定"-d recurse"参数相同。-s 或 –no-messages : 不显示错误信息。-v 或 –revert-match : 显示不包含匹配文本的所有行。-V 或 –version : 显示版本信息。-w 或 –word-regexp : 只显示全字符合的列。-x –line-regexp : 只显示全列符合的列。-y : 此参数的效果和指定"-i"参数相同。awk处理文本文件的语言,是一个强大的文本分析工具。awk ‘{pattern + action}’ {filenames}或者awk [-F field-separator] ‘commands’ input-file(s)awk -F, ‘$2 ~ /test/ {print $2"\t"$4}’ log.txt使用-F,的作用是每行按照,分割,$1-$2-$n就是分割的结果的对应顺序的值$2 ~ /th/就是需要分割的第二个数据需要和test匹配的上{print $2$4}输出分割结果的第二个和第四个处理的信息是log.txtawk -F ‘[:=]’ ‘{match($5,/.uc_name=(.)&extend=test./,a); print a[1]}’ log.txt-F ‘[:=]‘使用多个分隔符,先使用:分割,然后在对分割结果使用=二次分割。’{match($5,/.uc_name=(.)&extend=test./,a); print a[1]}‘对分割后的第五个结果处理,需要能够匹配上其中的正则,并把匹配结果放到数组a中,其中a数组的结果a[0]是全匹配的结果,a[1]是正则匹配的子表达式结果,并输出子表达式。sort、uniq、wc统计sort用于将文本文件内容加以排序。sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][–help][–verison][文件]-b 忽略每行前面开始出的空格字符。-c 检查文件是否已经按照顺序排序。-d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符。-f 排序时,将小写字母视为大写字母。-i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符。-m 将几个排序好的文件进行合并。-M 将前面3个字母依照月份的缩写进行排序。-n 依照数值的大小排序。-o<输出文件> 将排序后的结果存入指定的文件。-r 以相反的顺序来排序。-t<分隔字符> 指定排序时所用的栏位分隔字符。+<起始栏位>-<结束栏位> 以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。uniq用于检查及删除文本文件中重复出现的行列,需要与 sort 命令结合使用[一定要排序,不然去重无效,只会去除相邻的重复项]uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][–help][–version][输入文件][输出文件]-c或–count 在每列旁边显示该行重复出现的次数。-d或–repeated 仅显示重复出现的行列。-f<栏位>或–skip-fields=<栏位> 忽略比较指定的栏位。-s<字符位置>或–skip-chars=<字符位置> 忽略比较指定的字符。-u或–unique 仅显示出一次的行列。-w<字符位置>或–check-chars=<字符位置> 指定要比较的字符。wc计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][–help][–version][输入文件][输出文件]-c或–bytes或–chars 只显示Bytes数。-l或–lines 只显示行数。-w或–words 只显示字数。scp文件传输scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令[需要配置ssh登入,密码或者公钥免密]scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 […] [[user@]host2:]file2-1: 强制scp命令使用协议ssh1-2: 强制scp命令使用协议ssh2-4: 强制scp命令只使用IPv4寻址-6: 强制scp命令只使用IPv6寻址-B: 使用批处理模式(传输过程中不询问传输口令或短语)-C: 允许压缩。(将-C标志传递给ssh,从而打开压缩功能)-p:保留原文件的修改时间,访问时间和访问权限。-q: 不显示传输进度条。-r: 递归复制整个目录。-v:详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。-c cipher: 以cipher将数据传输进行加密,这个选项将直接传递给ssh。-F ssh_config: 指定一个替代的ssh配置文件,此参数直接传递给ssh。-i identity_file: 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。-l limit: 限定用户所能使用的带宽,以Kbit/s为单位。-o ssh_option: 如果习惯于使用ssh_config(5)中的参数传递方式,-P port:注意是大写的P, port是指定数据传输用到的端口号-S program: 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。实践线上日志结构:183.250.223.158 [16/Dec/2018:23:57:15 +0800] “GET /log?skdata=sdadadad111%22username%33%3ftestets%22fsfdsfssadadasd%34fdsfs%34 HTTP/1.0” 200 2 “-” “testiPhone/d64556” “120.188.90.136” “jsdgajdsad” “dasdadd” “test” “ceshi"分析数据命令:cat /home/logs/2018/12/access.2018-12-16.log | grep -E ‘skdata.*prodetail.*etype%2522%253A0.*eid.theme_prodetail.vs_theme’ | awk ‘{match($5,/.uc_name%2522%253A%2522(.)%2522%252C%2522etype./,a); print a[1]}’ | sort | uniq -c | wc -lcat查看某一天的日志文件,访问日志一般文件一天记录一份grep查看符合需求的访问日志,本实例使用正则匹配-E来筛选出符合要求的详情页访问日志,输出awk分析符合要求的详情页访问日志,把每行详情页访问日志中$5【空格分割的第五个字符串,本实例就是以/log?开头的字符串】去匹配一个正则.uc_name%2522%253A%2522(.)%2522%252C%2522etype.,并把结果放到变量a中【a[0]是全匹配,a[1]是第一个字表达式】,然后把匹配成功符合结果的a[1]输出sort将上述结果排序 [一定要排序,不然去重无效,只会去除相邻的重复项]uniq -c将上述结果去重wc -l查看总共结果数量 ...

December 18, 2018 · 2 min · jiezi