在Linux系统下通过多线程方式对日志文件进行操作

一、学习目标和计划?

在这里插入图片描述

二、vi, grep awk, sed以及一些Linux命令的学习

1. grep:在指定目录下搜索文件并对结果进行操作,适合单纯的查找匹配文本

1.grep after test.log > res.log:在test.log中找出含有after关键字的行,并将其保存到res.log中
2.find -name “*.log” ls:在当前目录下查找.log结尾的文件并展示信息
3.grep -n^r test.txt:查找以r开头的行

2.Linux

1.查看ip:ifconfig(win下是:ipconfig)
2.展开文件夹:ls,ll
3.ping ip:查看主机与虚拟机是否可以ping通, ping:www.baidu.com查看是否可以联网
4.route add default gw xx.xxx.xxx.xx:增加默认网关
5.文件/文件夹新建和删除:mkdir a,rmdir a, touch a.txt,rm (-rf) a.txt
6.文件夹改名:mv a.txt b.txt
7.拷贝文件aaa到usr目录下: cp /usr/tmp/aaa /usr
8.压缩:tar -zcvf ab.tar*:z是调用gzip压缩命令进行压缩,c是打包文件,v是显示运行过程,f是指定文件名
9.解压:tar -zxvf ab.tar
10.whereis:定位源代码文件 whereis ls
11.网络管理:vi /etc/sysconfig/network 主机名, vi /etc/sysconfig/network-scripts/ifcfg-eth0 网络信息配置
12.查看目录:pwd
13.查看当前运行的进程:ps -ef
14.查看端口:netstat -an
15.清屏:ctrl+l
16.df -l:显示磁盘所用情况
17.df -a:文件系统列表
18.编译:g++ test.c -o test
19.运行:./test
20.检查vim是否安装:rqm -qa|grep vim
21.查看文本:cat test.txt
22.文件操作:more,less,cat,tail
23.合并文件:cat 1.log 2.log > 3.log

3.vi

1.esc+shift:切换模式
2.q:退出
3.q!:强制退出
4.wq:保存修改
5.i:进入插入模式
6.dd:删除一行
7.yy:复制光标所在行
8.p:粘贴到光标所在位置
9.r/R:替换光标所到之处的字符
10.ctrl+g:显示所在行
11./+关键字:查找字符
12.:set+nu:显示文本每一行的行号

4.sed:利用脚本处理文件,适合编辑匹配到的文本

1.选项:
-h:显示帮助信息
-n:显示script处理后的结果,一般与p连用
-e:直接在指令列模式上进行sed,不修改源文件
-i:修改文件,但不输出到终端
2.命令:
a:增加:sed -3 ‘4 a newline’ test.txt(第四行增加一个newline)
c:取代:sed -e ‘2,5c abc’ test.txt(2-5行用abc替代)
d:删除:sed -e ‘2,5d’ test.txt
i:插入:和a相比,i是在前面插入
p:打印:sed -n ‘3,5p’ test.txt

sed -n '1~2p’test.txt>>a.log:累加
sed -n '1~2p’test.txt>a.log:覆盖
sed -e ‘s/1/one/’ test.txt:把全局的1替换为one
sed “2c hello” test.txt:用hello替换第二行

5.awk:在文件和字符串中基于指定规则和抽取信息

6.GDB调试

编译命令:g++ -g test.cpp -o test
开始调试:gdb test
退出:quit
在第五行加入断点:break(b) 5
运行:run
单步调试:next
(gdb): b/r/c/n/p/l/q/until/finish

三:多线程处理文本

1.文件读写操作

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#define ID_LEN    36

using namespace std;

int main()
{
   
    fstream f("res1.log");
    string line;
    vector<string> vec; //save all the new line
    int number = 0; // time of ip request id
    while (getline(f, line))
    {
   
        string part_1, part_2, newline;
        int begin_1, begin_2, end_1, end_2;

        begin_1 = line.find("did=") + 4;  // did
        // end_1 = begin_1 + 36;
        part_1 = line.substr(begin_1, ID_LEN);

        begin_2 = line.find("host:") + 5;
        end_2 = line.find(">") - 5;
        int len_2 = end_2 - begin_2;
        part_2 = line.substr(
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值