shell之内容替换及获取行数

#获取时间
[root@localhost ~]# echo "["`date "+%Y-%m-%d %H:%M:%S"`"]"
[2023-08-10 18:35:01]

#替换
[root@localhost ~]# echo "she : 'sss' end" | awk '{gsub("sss","ggg");gsub(":","is");print}'
she is 'ggg' end

#替换第一行
[root@localhost ~]# cat text.txt
1reportdate:2020-02-12
 shhdh
[root@localhost ~]# sed -n -r '1s/(.).*/\1/p' text.txt
1

#批量替换
[root@localhost ~]# cat text.txt
1reportdate:2020-02-12
1shhdh
 shhsh
[root@localhost ~]# sed -i 's/h$/\!/g' text.txt
[root@localhost ~]# cat text.txt
1reportdate:2020-02-12
1shhd!
 shhs!

#查看内容
[root@localhost ~]# cat text.txt
1reportdate:2020-02-12
1shhd!
 shhs!
#获取第一行字符
[root@localhost ~]# awk 'NR==1{print substr($1,4,8)}' text.txt
portdate
#获取日期
[root@localhost ~]# awk '/reportdate/{split($0,s,":");print s[2]}' text.txt
2020-02-12
#获取分页符数量
[root@localhost ~]# awk '/\f/' text.txt |wc -l
0
#获取行数
[root@localhost ~]# awk 'END{print NR}' text.txt
3
#替换换页符
awk '{gsub("\f","");print}' text.txt >text1.txt
#字符集转换
iconv -c -f utf-8 -t gbk text.txt >text1.txt

#删除3天前的文件夹
find /etl -mtime +3 -type d -print|xargs rm -rf
#删除3天前的文件
find /etl -mtime +3 -type f -print|xargs rm -rf

你可能感兴趣的:(shell,linux)