Linux之常用命令awk,sed ,grep以及如何查看日志来定位具体问题

 

文本处理命令

 Awk:功能比较强大,可以有比较强大的计算逻辑在里面,sed主要用来对文本进行替换和删除,逐行扫描。grep只是单纯查找

简介 

https://en.wikipedia.org/wiki/AWK

https://www6.software.ibm.com/developerworks/education/au-gawk/au-gawk-a4.pdf

linux 和 mac 的兼容性
https://stackoverflow.com/questions/24332942/why-awk-script-does-not-work-on-mac-os-but-works-on-linux


cat my_shoping.list
No    Item_Name        Unit_Price    Quantity    Price
1    Mouse            #20,000           1        #20,000
2     Monitor            #500,000       1        #500,000
3    RAM_Chips        #150,000       2        #300,000
4    Ethernet_Cables            #30,000           4        #120,000

from file or as filter

pattern 

awk '{print}' my_shoping.list

awk '//' my_shoping.list

awk '/Mouse/ || /Mon/  {print }' my_shoping.list

awk '/Mouse/ && /Mon/  {print }' my_shoping.list

awk '/M/ && /20,000/  {print }' my_shoping.list

awk '/green/ { print }' sample
 
awk '/!.*!/' sample

awk '$3 ~ /is/ ' sample

awk 'BEGIN {FS=":"} $7 !~ /false/ {print $5}' /etc/passwd

cat my_shoping.list| awk '//' 

awk '{ print $1 } ' sample

awk ' BEGIN { FS = "!" } { print $1 } ' sample

awk ' BEGIN { FS = "," } { print $1 } ' sample

key words

$0, $1, NR, NF

sed

https://likegeeks.com/sed-linux/


 替换

 sed 's/unix/linux/' geekfile.txt
 
 sed 's/unix/linux/2' geekfile.txt
 
 sed 's/unix/linux/g' geekfile.txt
 
  sed '3 s/unix/linux/' geekfile.txt
  
  cat geekfile.txt
  
  sed 's/unix/linux/p' geekfile.txt
  
  sed -n 's/unix/linux/p' geekfile.txt
  sed '1,3 s/unix/linux/' geekfile.txt

 删除

  sed '2d' geekfile.txt
  sed '$d' geekfile.txt
  sed '1,2d' geekfile.txt

  sed '2,$d' geekfile.txt

  sed '/learn/' geekfile.txt
  
  sed '/learn/d' geekfile.txt
  
 修改原文件

sed  -i bak 's/unix/linux/g' geekfile.txt


grep

https://www.linuxtechi.com/linux-grep-command-with-14-different-examples/

grep daniel /etc/passwd

grep root /etc/passwd

grep root /etc/passwd /etc/gshadow

grep -l Price ./*

grep Price ./*

grep -n Price ./*

grep -vn Price ./*

grep ^No food_list.txt

grep No food_list.txt

grep '^No' food_list.txt

grep ^No food_list.txt

grep '^1' food_list.txt

grep 'Price$' food_list.txt

grep -r root /etc

grep -rl root /etc

grep -i price ./*

man grep

grep -e "linux" geekfile.txt

grep -e "linux" -e

grep -e "great" -e

grep -f grep_pattern /etc/passwd

grep -f grep_pattern1 geekfile.txt


grep -f grep_pattern1 geekfile.txt

grep -f grep_pattern1 geekfile.txt


 head, tail

  head -n 2 food_list.txt
  
  tail -n 2 food_list.txt
  
  tail -c 20 food_list.txt
  
  
  head -c 20 food_list.txt
  
  
  tail -fn 200 food_list.txt
  


cat , less,more

man 


cat 连接多个文件,一次显示

cat food_list.txt my_shoping.list


less is more

快捷键提示 h

## sort, uniq

cat sort.txt
 
sort sort.txt

sort -u sort.txt

uniq sort.txt

uniq sort.txt

sort -u sort.txt

vim sort-nrkt.txt

cat sort-nrkt.txt

sort -nk 2 -t: sort-nrkt.txt

sort -nkr 3 -t: sort-nrkt.txt

sort -nrk 3 -t: sort-nrkt.txt

uniq -d sort.txt

uniq -cd sort.txt

uniq -c sort.txt

uniq -d sort.txt


统计响应时间超过5秒的接口: cat  www-ccbscf-access1.log | awk  'BEGIN { FS = "[|]+"}; /\|\|/&& /09\/Apr/ && $NF >5 {print $2, $3, $NF} '|sort -t ' ' -rnk 6

 日志查看


## 确定哪个日志

ls -lrt

## 先到api-web


http://test2.ccbscf.com/api/web/biz/v1/corps/invoices/list?name=&invoiceType=&mailBusinessType=INVOICE&page=1&pageSize=10


http://12.0.216.145:19001/eureka/apps


less  ccbscf-api-web.log

可以先到 日志文件的最后,倒序查找


## 如何区分出特定请求的日志

tail -fn 200 ccbscf-api-web.log |grep -C 10 'biz/v1/corps/invoices/list'


grep -C 5 'biz/v1/corps/invoices/list' ccbscf-api-web.log|tail -10

# 后台执行

nohup/setsid/&


  ping www.taobao.com
  
  nohup ping www.taobao.com
  
  nohup ping www.taobao.com &
 (ping www.ibm.com &) 
 screen -S ping-test

 screen -l
  
 screen ls
  
 screen ls
 
 screen -ls
 
 screen -rAd ping-test

screen -rAd ping-test

你可能感兴趣的:(201904)