用 awk or sed 指令篩選文字檔欄位內容

主要把 ip 列出來…其它的就以下指令加入

find 出檔案內容以及sed方式修改內容

find ./ -type f |grep -v svn-base|xargs -n 1 -i grep -l qltx.live {}

find ./ -type f |grep -v svn-base|xargs -n 1 -i grep -l qltx.live {}|xargs -n 1 -i sed -i 's/qltx.live/qltx.tv/g' {}

除最後一行之外的每一行的末尾,bash如何添加逗號?

sed '1s/^/[/;$!s/$/,/;$s/$/]/' file

參考:https://hant-kb.kutu66.com/bash/post_12640476

sed
cat input.txt | sed 's/^/"/;s/$/",/'

awk
cat input.txt | awk '{print "\""$0"\","}'

xargs
cat input.txt | xargs printf '"%s",\n'

https://blog.csdn.net/bdss58/article/details/54584010

清空列與^M

cat DomainList.txt | grep -v "#" | tr -d "\r" | sed 's/,/\n/g'

cat ok.csv | awk -F " " '{print $1,"ansible_port=20022 ansible_ssh_user=root ansible_ssh_pass="xxx" ansible_ssh_host="$1 }'

zhangjiakou_dx_0001_106.8.148.71 ansible_port=20022 ansible_ssh_user=root ansible_ssh_pass=xxx ansible_ssh_host=106.8.148.71 huaibei_yd_0001_112.28.223.88 ansible_port=20022 ansible_ssh_user=root ansible_ssh_pass=xxx ansible_ssh_host=112.28.223.88
 

特殊字元取代

sed -i 's,map / http://127.0.0.1,#map / http://127.0.0.1,g' remap.config
sed -i 's/#0/0/g' /etc/crontab

如何拿掉上引號

1 cat waver-list | grep "ip"| awk -F " " '{ print $2}' | awk -F ',' '{ print $1}' | sed 's/^["]*//;s/["]*$//'
 
shell命令去除字符串里双引号

echo \"refs/changes/84/727284/1\" | sed 's/\"//g'

加引號 (ali 防火牆匯入方式)

cat waver-server-list.txt | awk -F " " '{print "intranet"",""ingress"",""Accept"",""TCP"",""10050/10050"",""1"","$1","""","""","""","""","""","""","""","""","""","""","""",""2019-11-29T10:17:48Z""\""}'
 
result:
intranet,ingress,Accept,TCP,10050/10050,1,119.6.234.154,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,125.77.129.140,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,111.63.161.138,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,111.6.188.27,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,61.147.228.85,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,106.225.228.33,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,117.174.176.100,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,14.204.185.220,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,220.167.101.158,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,59.63.81.126,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,120.233.80.212,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,111.43.178.185,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,112.90.242.76,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,120.241.97.182,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,120.39.211.231,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,122.70.155.125,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,120.220.37.209,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,101.206.204.201,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,221.178.32.236,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,183.131.168.209,,,,,,,,,,,,2019-11-29T10:17:48Z"
intranet,ingress,Accept,TCP,10050/10050,1,36.35.39.29,,,,,,,,,,,,2019-11-29T10:17:48Z"

先取 raw data , 再用 awk 方式來取要的內容

cat login.txt| awk  '{ {split($7,a,":")}print $1,$2 "," $5 a[1] "," $9  }'

result:
[2019-02-01 00:00:00],xxxxxxxxxxxxxxxxxxx,24.27.0.0,2,

 cat login.txt| awk    '{ {split($7,a,":") }  print substr($1,2 ), substr($2,1,8) "," $5 a[1] "," $9 }'


result:
2019-02-01 00:00:00,xxxxxxxxxxxxxxxxxxx,24.27.0.0,2,

Comments

  1. michael_hong says:

    dd