最近博主被分配了一项紧急任务,事情有些紧急,花了两天的时间学习Shell,并完成了一项任务.自认为还算不错,
接下来分享下处理这个任务中所编写的一些脚本,就算留作纪念吧
用到的技术点
0.常用的 Linux命令 cat / grep / sed / cd / wc
1.shell 流程控制
2.shell 条件判断
3.shell 变量
5.shell 数组
6.正则匹配
将两个文件下的指定文件合并放置到一个新的文件夹下面
#!/bin/sh
date=0
dir1="/data/logs/d19_201606/06/selectedlogs/82/baselogs/newlogs_tmp/newlogs/"
dir2="/data/logs/d19_201606/80_advanced/"
outdir=""
for((i=1;i<=24;i++));do
if((i<10));then
date="0$i"
else
date="$i"
fi
#echo $date
cat ${dir1}stm-collect.cn.miaozhen.com_201607${date}_82_new.log ${dir2}201607${date}.log >> /data/logs/sm_new/82_new/201607${date}_82.log
done
重命名符合条件的文件(批处理)
#!/bin/sh
in=20160701
while(($in<20160725))
do
filename="stm-collect.cn.miaozhen.com_""$in"".log"
outname="stm-collect.cn.miaozhen.com_""$in""_.log"
mv $outname $filename
let "in++"
done
利用sed与正则替换符合条件的串
#!/bin/sh
logFile="copy_05_06.log"
date="02"
sed -i "s/[0-9]\{2\}\/\(May\|Jun\|Jul\)/$date\/Jul/g" $logFile
grep -v "$date/Jul" $logFile #检验有没有删除干净
利用grep -v 检查日志文件是否有不满足规则的行
#!/bin/sh
dir="/data/logs/d19_201606/06/ selectedlogs/82/baselogs/newlogs/"
date_count=25
sitelist=(82)
echo ${sitelist[0]}
cd "${dir}" #进入制定路径
date="00"
for((i=1; i<25; i++));do
if((i<10));then
date="0$i"
else
date="$i"
fi
echo $date
grep -v "${date}/Jul" *201607${date}_82_new.log >> error.log
done
========一些不太常用到的脚本=================
==============================================
从日志文件中随机选取几条记录写入到另一个文件中
版本一
#!/bin/sh
date="04"
file="copy_05_06.log"
outfile="Jul_""$date.log"
declare lines=0; #日志文件中剩余的日志条数
function totalLines(){
result="`wc -l $file`" #如果想从命令中获取数据的话,也可以用 $()
arr=($result) #将结果转换为数组
lines=${arr[0]} #获取第0个参数,行号
#echo $lines #打印总的行数
}
sum=$(($RANDOM%8+8)) #sum记录了随机选取的条数
echo "lines has been selected Num:$sum"
echo "before delete: $(wc -l $file)"
for((i=0;i<$sum;i++));do
#echo $i
totalLines;
#echo ${lines}
randomline=$(($RANDOM%$lines))
sed -n "${randomline}p" "$file" >> $outfile #随机取一条数据追加到outfile
sed -i "${randomline}d" "$file" #删除被取出取的那一行
#echo $randomline
done
echo "after delete: $(wc -l $file)"
echo "selected lines: $(wc -l $outfile)"
版本二
1、合并 cat file1 file2 > file3
2、去重 sort file3 | uniq >> file4
3、随机抽取 shuf -n12 file4 > file5 查行数wc -l
4、去掉a中包含b的部分
cat b.txt a.txt | sort| uniq -u > file
随机删除日志文件中的数据(批处理方式)
#! /bin/bash
rate=$((RANDOM%7+85))
indir="/mnt/hgfs/Share_Windows_Linux/"
outdir="/mnt/hgfs/Share_Windows_Linux/"
indate=(18 17 16 18 28 17 16 30 27 19)
outdate=(01 02 03 04 05 06 07 08 09 10)
cd "$indir"
num=0
for((i=0;i<10;i++));do
cat stm-collect.cn.miaozhen.com_201606${indate[i]}_selected.log | while read LINE
do
num=$(($RANDOM%100))
#echo ${num}
if((${num} <= ${rate}));then
#echo ${num}
echo $LINE >> ${outdir}stm-collect.cn.miaozhen.com_201607${outdate[i]}_new.log
fi
done
done
awk修改cid,对原有的cid进行随机+n操作
#!/bin/bash
sour="data.txt"
out="data_new.txt"
ct=$(wc -l $sour | awk '{print $1}')
echo "source file lines : $ct"
lines=$(( ($RANDOM%10+90)*ct/100 ))
echo "should be change lines : $lines"
awk -v num=$lines -F'&' '
BEGIN{
srand();
}
{
if(NR<=num)
{
split($2,a,"=");
ct=int(rand()*100%9);
a[2]=a[2]+ct;
gsub(/cid=[0-9]*/,"cid="a[2],$0);
#$2=a[1]"="a[2];
}
print $0;
}
' $sour > $out
old=$(wc -l $sour | awk '{print $1}')
new=$(wc -l $out | awk '{print $1}')
echo "old file has $old lines"
echo "new file has $new lines"