vi/vim/sed/文件格式等常用命令

1、vi a.txt 进去文件,在没有进入编辑模式(i)下 操作如下:

编辑:

复制当前行:  yy
粘贴:p
删除当前行:dd
清空文件: :%d
回退: u
撤销回退:ctrl+r

粘贴模式::set paste  (解决复制粘贴格式错乱)

光标移动:  

光标到页首: H
光标到页尾: L
光标到中部:M
显示行号:  :set nu
取消显示行号: :set nonu
跳转到指定行(例如第5行):  :5  
翻上一页: ctrl+b
翻下一页: ctrl+f

查找:

查找字符串(例如aaa): /aaa
查找下一个(查找操作回车后输入): n
查找上一个(查找操作回车后输入): N

替换( / 可以用 # 代替):

全文替换(将apple/orange/mango 替换为fruit): :%s/apple\|orange\|mango/fruit/g
当前行(s前没有%)全替换(将foo替换为bar): :s/foo/bar/g   
当前行替换首次(没有g结尾)出现:  :s/foo/bar/
不区分大小写替换(i标志):  :s/foo/bar/i
指定行替换(3~5行)所有出现:  :3,10s/foo/bar/g
相对行替换(当前行~接下来4行): :.,+4s/foo/bar/g
当前行到结尾替换: :.,$s/foo/bar/g

  /g结尾就是global全局修改

s开头表示第几行开始,%s全部行,2s第二行

2、sed 命令

打印: 

输出指定行(打印第二行):  sed -n  '2p'   a.txt    
输出非行(除了2到4行的全部内容): sed  '2,4d' a.txt
替换输出(替换第 3 行中 89 替换成 99 数据) :  sed '3s/89/99/g' a.txt

替换文件本身:

不会打印(替换第 3 行中 89 替换成 99 数据): sed -i '3s/89/99/g' a.txt
在所有行首添加222 : sed 's/^/222/g' 
在所有行末添加222: sed 's/.$/222/g' 

  /g结尾就是global全局修改

s开头表示第几行开始,s全部行,2s第二行

^行首占位符,.$行尾占位符

3、awk命令

cat scallop.properties |awk -F '=' '{if($2>0&&NR>1) print $2}' 输出>1 行 第2个字段>0 的第二个字段
cat scallop.properties |awk -F '=' 'NR>1&&$2>0 {print $2}' 输出>1 行 第2个字段>0 的第二个字段
awk -F '=' '/200/ {print $0}' scallop.properties  输出包含200 的整行, 
cat scallop.properties |awk '/com/&&$17>1 {print($0)}'  包含com&&第17个字段>1 ,则打印整行
awk 'BEGIN {total=0;FS=":";print "现在开始统计"} {total=total+$2; print("计算:"$2);print("结果:"total)} END {print total}' scallop.properties
awk '{split($0,a,":");print a[1],a[2],a[3]}' 分割

- begin {定义total=0,FS分隔符,等前置条件 } { 循环体 } end {结果输出} 待处理文件

// 更多示例
cat stdout.log|awk '/time total/ {if((substr($9,7)+0)>10000) print($0)}'
cat stat-log.log |awk '/\$Proxy9/ {if($10>100) print $0}'
cat stat-log.log |awk '/\$Proxy9/ {if(substr($6,12,4)>9566) print $0}'
awk '{print $6}' traceIds.log |uniq -c 排查重复
zcat track-2019-05-29-1.log.gz |awk -F '"startTime":' '{split($2,a,",");if(a[1]>1559121300000 && a[1]<1559121600000) print $0 }' |wc -l
NR 当前行号,$NF结尾字段

4、curl

curl https://www.example.com 直接get请求
curl -b 'foo1=bar;foo2=bar2' https://google.com  添加cookie(-b)
curl -b cookies.txt https://www.google.com   添加cookie,也可以本地文件
curl -d 'login=emma&password=123' -X POST https://google.com/login    post请求(包含-d 会默认post请求,所以可以删除-x post)
curl -d '@data.txt' https://google.com/login  post请求,本地获取参数
curl -F 'file=@photo.png;type=image/png' https://google.com/profile  上传二进制文件(包含-F 会默认 Content-Type: multipart/form-data)
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login  指定header(-H)
curl -k https://www.example.com  不会检查服务器的 SSL 证书是否正确
curl -L -d 'tweet=hi' https://api.twitter.com/tweet 让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
curl -O https://www.example.com/foo/bar.html 将服务器回应保存成文件,文件名为bar.html
curl -u 'bob:12345' https://google.com/login 设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
curl -v https://www.example.com  输出通信的整个过程,用于调试
curl -v -XPOST --data-binary @cpu.info http://101.11.xxx.xxx:9091/metrics/job/pushgateway  请求中的数据为纯二进制数据,value如果是@file_name,则保留文件中的回车符和换行符,不做任何转换
curl --location --request POST 'http://mediates/queryCode'--header 'Content-Type: application/json' --data '{"body": {"aaa": "avule"}}'

5、sort排序

sort -n e3.txt     // 对e3进行排序,正序(默认),按数字排序,无数字即为0(从头开始取,直到非数字结束)
sort -nr e3.txt     // 对e3进行排序,倒序,按数字排序,无数字即为0(从头开始取,直到非数字结束)
sort -nr -k 2 e3.txt     // 对e3进行排序,倒序 , 取第2个字段(默认空格为分隔符),按数字排序
sort -nr -t ',' -k 2 e3.txt     // 对e3进行排序,倒序 , 取第2个字段, 以逗号为分隔符,按数字排序
sort -nr -t ',' -k -b 2 e3.txt     // 对e3进行排序,倒序 , 取第2个字段, 以逗号为分隔符,按数字排序,忽略开头空

6、 wc统计

head -n 100 e3.txt // 查看前100行
tail -100f e3.txt  // 查看尾100行
wc -l  e3.txt  //  就是查看文件里有多少行
wc -w  e3.txt  //  就是查看文件里有多少词,空格为分隔符(hello word 为两个词)
wc -L e3.txt  //  文件里最长的那一行是多少个字
wc -c e3.txt  // 就是查看文件里有多少字
wc e3.txt  //  默认统计 -lwc 行数、词、字

7、 文件格式转换

查看文件编码格式:file static1.c 
修改文件编码:iconv -f gbk -t utf-8 static1.c  -o static1.c 
[描述] 
-f encoding :把字符从encoding编码开始转换。 
-t encoding :把字符转换到encoding编码。 
-l :列出已知的编码字符集合 
-o file :指定输出文件 
-c :忽略输出的非法字符 
-s :禁止警告信息,但不是错误信息 
--verbose :显示进度信息 
-f和-t所能指定的合法字符在-l选项的命令里面都列出来了。 

  

还可以:

vi  static1.c

 :set fileencoding=utf-8 命令来设置文件的编码格式

 

 

 

posted @ 2023-07-14 11:05  我的天啊~  阅读(162)  评论(0编辑  收藏  举报