简体   繁体   中英

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? (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

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.

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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