linux shell终端最常用的三个命令
#!/bin/bashcat access.log | while read line; do# 用 cut 方式取得每一行的时间,并赋予变量t=$(echo $line | awk {'print $1'})t1=$(echo $line | awk {'print $1'})echo $t, $t1 >> new.logdone
1.grep
1.1 查看匹配行
grep -C 5 'parttern' inputfile //打印匹配行的前后5行 --- continuegrep -A 5 'parttern' inputfile //打印匹配行的后5行 ---- aftergrep -B 5 'parttern' inputfile //打印匹配行的前5行 --- before
2. sed
2.1 在某一行后面添加新行
sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串 HELLO LINUX! #testfile文件原有的内容 Linux is a free unix-type opterating system. This is a linux testfile! Linux test newline
2.2 删除文件的第二行到第五行
[root@www ~]# nl /etc/passwd | sed '2,5d'1 root:x:0:0:root:/root:/bin/bash6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
只要删除第 2 行nl /etc/passwd | sed '2d' 要删除第 3 到最后一行nl /etc/passwd | sed '3,$d'
在某一行后面添加新行
[root@www ~]# nl /etc/passwd | sed '2a drink tea'1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologindrink tea3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
2.3 搜索含有某个关键字的行
搜索 /etc/passwd有root关键字的行nl /etc/passwd | sed '/root/p'1 root:x:0:0:root:/root:/bin/bash1 root:x:0:0:root:/root:/bin/bash2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh3 bin:x:2:2:bin:/bin:/bin/sh4 sys:x:3:3:sys:/dev:/bin/sh5 sync:x:4:65534:sync:/bin:/bin/sync....下面忽略 如果root找到,除了输出所有行,还会输出匹配行。使用-n的时候将只打印包含模板的行。nl /etc/passwd | sed -n '/root/p'1 root:x:0:0:root:/root:/bin/bash
2.4 删除包含有该字段的行
删除/etc/passwd所有包含root的行,其他行输出nl /etc/passwd | sed '/root/d'2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh3 bin:x:2:2:bin:/bin:/bin/sh....下面忽略#第一行的匹配root已经删除了
2.5 删除某一区间行、将搜索到的关键字替换
多点编辑一条sed命令,删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshellnl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'1 root:x:0:0:root:/root:/bin/blueshell2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh-e表示多点编辑,第一个编辑命令删除/etc/passwd第三行到末尾的数据,第二条命令搜索bash替换为blueshell。
3. awk
3.1 以冒号分割并打印第一列
awk -F : '{ print $1 }' /etc/passwd 输出为 :root bin daemon
3.2 显示/etc/passwd的第1列和第7列,用逗号分隔显示,所有行开始前添加列名start1,start7,最后一行添加,end1,end7
awk -F ':' 'BEGIN {print "start1,start7"} {print $1 "," $7} END {print "end1,end7"}' /etc/passwd 输出为 : start1,start7 root,/bin/bash bin,/sbin/nologin
3.3统计/etc/passwd文件中,每行的行号,每行的列数,对应的完整行内容
awk -F : ‘{ print NR " " NF " " $0 }’ /etc/passwd
NF 浏览记录的字段个数
NR 已读的记录数
输出为
1 7 root:x:0:0:root:/root:/bin/bash 2 7 bin:x:1:1:bin:/bin:/sbin/nologin
3.4 支持条件操作,正则表达式匹配
显示/etc/passwd中有daemon的行
awk -F ':' '$0 ~ /daemon/' /etc/passwddaemon:x:2:2:daemon:/sbin:/sbin/nologin
3.5 awk -F ‘:’ ‘{ if ($1 > “d”) { print $1 } else { print “-” } }’ /etc/passwd
root -daemon -lp
3.6 批量杀死进程
ps -aux | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9
execpath=$(cd "$(dirname "$0")"; pwd)echo "current executed path is : ${execpath}"cd ${execpath}killname=mongoecho "before status"ps -elf | grep ${killname} | grep -v grepecho "before status"PROCESS=`ps -ef|grep ${killname} | grep -v grep | grep -v PPID|awk '{print $2}'`for i in $PROCESSdoecho "Kill the process [ ${killname} ]"kill -9 ${i}doneecho "after status"ps -elf | grep ${killname} | grep -v grepecho "after status"
4.其它命令
统计文本文件行数wc -l test1.txt
2.uniq -c, --count #在每行前显示该行重复次数,只统计前后相邻
eg:
this is a testthis is a testthis is a testi am tanki love tanki love tankthis is a testwhom have a tryWhoM have a tryyou have a tryi want to abroadthose are good menwe are good men
sort filename 统计文本文件每行出现的次数
以上两个命令一般同时使用
sort bb.txt | uniq -c
效果如下:
1 i am tank2 i love tank1 i want to abroad4 this is a test1 those are good men1 we are good men1 whom have a try1 WhoM have a try1 you have a try
more /less /head