linux命令总结
文本提取
awk -F ‘,’ ‘{print $(NF-2)}’ logs.txt
awk begin{RS=EOF}’{gsub(/\n/," ");print}’ uid.txt
以冒号分割,取第二个字段
awk -F ‘:’ ‘{print $2}’ sd.txt
统计文本的行数
文件test1.txt有17行
方法一:
awk ‘{print NR}’ test1.txt | tail -n1
管道前所有行号,管道后则打印最后一行
或者:
awk ‘END{print NR}’ test1.txt
方法二:
grep -n ‘’ test1.txt | awk -F : ‘{print $1}’ | tail -n1
或者:
grep -n ‘’ test1.txt | awk -F : ‘END{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲' 方法三: sed -n '=’ test1.txt
备注:统计文件的行数,等同于cat test1.txt | wc -l 命令(下面方法中所述),具体点就是-n抑制输出, $到最后一行,=打印行号。
方法四:
wc -l test1.txt
或者:wc -l test1.txt | awk ‘{print $1}’
又或者:cat test1.txt | wc -l
文本处理时,经常要删除重复行,下面是三种方法
第一,用sort+uniq,注意,单纯uniq是不行的。
sort -n test.txt | uniq
第二,用sort+awk命令,注意,单纯awk同样不行,原因同上。
sort -n $file | awk ‘{if($0!=line)print; line=$0}’
第三,用sort+sed命令,同样需要sort命令先排序。
sort -n
f
i
l
e
∣
s
e
d
′
file | sed '
file∣sed′!N; /^(.*)\n\1$/!P; D’
查看cpu核数
cpu个数
cat /proc/cpuinfo| grep “physical id”| sort| uniq| wc -l
cpu核数
cat /proc/cpuinfo| grep “cpu cores”| uniq
内存
free -m
ps -ef|grep test|grep -v grep |awk ‘{print $2}’
ps -ef|grep test|grep -v grep |awk ‘{print $2}’|xargs kill -9
nohup指定输出的目录文件
原命令:
nohup /home/work/… Push.php &
重定向:
nohup /home/work/… Push.php >> pushDout0207.out 2>&1 &
0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) ;
2>&1是将标准错误(2)重定向到标准输出(&1),标准输出(&1)再被重定向输入到output文件中。
批量杀死进程
ps -ef|grep ftp://ftprmb:Z5YqkGebhfaR9k7r@yq01 | awk ‘{print $2}’|xargs kill -9
批量删除git 分支
git branch | grep ‘test’ |xargs git branch -D