简体   繁体   English

bash-从文本文件中删除多行不同的文本

[英]bash - remove multiple different lines of text from a text file

I'm working with lots of log files, and most log files have lots of repeating strings that are logged multiple times. 我正在使用许多日志文件,大多数日志文件都有很多重复的字符串,这些字符串被多次记录。 To make the logs easily viewable for others who don't have much to do with such things (for myself also), i've wanted to make a script that rips out some text lines that can cause a 'false alarm' to other people. 为了使与其他事情无关的其他人(也对我自己)而言,日志易于查看,我想制作一个脚本,该脚本会删除一些文本行,这些文本行可能对其他人造成“虚假警报” 。 ("Hey admin, i have these errors here multiple times"; > "Sigh, these errors don't mean anything" kind of way) (“嗨,管理员,我在这里多次遇到这些错误”;>“抱歉,这些错误没有任何意义”)

Is there some bash code with grep, cat or awk that can get rid of lots of different text lines, without having to go through the document over and over again for each line to be removed? 是否有一些带有grep,cat或awk的bash代码可以消除很多不同的文本行,而不必一遍又一遍地删除每行要删除的文档? (basically remove all garbage lines in one swoop) (基本上一举删除所有垃圾行)

Example, i'll mark the lines that i want removed in bold : 例如,我将要删除的行用粗体标记:

One thing I don't know why 一件事我不知道为什么

It doesn't even matter how hard you try 努力程度甚至都没有关系

Keep that in mind, I designed this rhyme 请记住,我设计了这首韵律

To explain in due time 适时解释

All I know 我所知道的

time is a valuable thing 时间是一件宝贵的事情

Watch it fly by as the pendulum swings 看着它随着钟摆的摆动而飞过

Watch it count down to the end of the day 看着它倒计时到一天结束

The clock ticks life away 时钟敲打生活

It's so unreal 太虚幻了

Didn't look out below 没看下面

Watch the time go right out the window 看着时间窗外

Trying to hold on but didn't even know 试图坚持,但甚至不知道

Wasted it all just to 浪费这一切只是为了

Watch you go 看你走

Sorry about the Linkin Park Lyrics, listening to the Radio while trying to solve a problem gives some bad examples sometimes :P 对不起Linkin Park歌词,在尝试解决问题的同时收听广播有时会给出一些错误的例子:P

Are all these lines removable in one command? 所有这些行都可以在一个命令中删除吗? Many thanks if somebody knows how. 如果有人知道,非常感谢。

grep -v "<string1>\|<string2>\|<stringN>" /path/to/file

It removes the lines provided in not_wanted array. 它删除not_wanted数组中提供的行。

#!/bin/bash
    exec < example.txt
    not_wanted[0]="It doesn’t even matter how hard you try"
    not_wanted[1]="time is a valuable thing"
    not_wanted[2]="The clock ticks life away"
    not_wanted[3]="It’s so unreal"
    not_wanted[4]="Trying to hold on but didn’t even know"

    while read line; do
        for i in "${not_wanted[@]}"; do
            if [ "$line" == "$i" ]; then unset line; break; fi
        done 
        if [ "$line" ]; then echo "$line"; fi
    done

将不需要的行放入文件中,然后

grep -v -f not.wanted filename > smaller.file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM