csestony
csestony

Reputation: 235

Matching all Characters between two Strings using Sed or any Unix command

I have a log file and i want to delete all com.test.data text in my log file.In example below I want all occurrences in bold to be striped out.

The new lines after the text I want are causing havoc, removing all \n would ruin the entire.log file.

I have tried grep -e 'com.test.data' debug.log --color -v > test.log.

Also tried sed 's/com.test.data -\(.*\)2016-01/\1/' debug.log > test.log.

Also tried sed '/'com.test.data -'/,/2016-01/ s/[a-z]*// p' debug.log > test.log

All seem to fail.

My plan was find all text between 'com.test.data -' and '2016-01' replace all of this with 'empty'(delete all). Then take my file and use grep to give me all lines except 'com.test.data -'. Doing this first seems to get me closer.

INPUT

2016-01-05 14:45:05,264 com.test.data - blah blah blahsd

afdadfasdfsd
sdsdfsdffssfddsf

dfsdsfdfsdfdsdfs
fsdsdfdfs
2016-01-05 14:46:05,264 com.test.rest - Testing 123
2016-01-05 14:47:05,264 com.test.data - blah blah blahsd

afdadfasdfsd
sdsdfsdffssfddsf
dfsdsfdfsdfdsdfs

fsdsdfdfs
2016-01-05 14:46:05,264 com.test.rest - Testing 123

OR USE

sed 's/^2016-01/@/' debug.log > test.log //Add @ to beginning of file

Now its just finding text from com.test.data ---> @

NEW INPUT TO MAKE THINGS EASIER

@-05 14:45:05,264 com.test.data - blah blah blahsd

afdadfasdfsd
sdsdfsdffssfddsf

dfsdsfdfsdfdsdfs
fsdsdfdfs
@-05 14:46:05,264 com.test.rest - Testing 123
@-05 14:47:05,264 com.test.data - blah blah blahsd

afdadfasdfsd
sdsdfsdffssfddsf
dfsdsfdfsdfdsdfs

fsdsdfdfs
@-05 14:46:05,264 com.test.rest - Testing 123

OUTPUT

2016-01-05 14:46:05,264 com.test.rest - Testing 123

2016-01-05 14:46:05,264 com.test.rest - Testing 123

Any ideas?

Upvotes: 1

Views: 152

Answers (4)

peak
peak

Reputation: 116750

If the following script does not do exactly what you want, it should be very easy to adapt it to your requirements. It is also portable, efficient, and maintainable:

awk '
  BEGIN {ok=1}
  /^ *@-/ { if ($3 == "com.test.data") {ok=0; next} else {ok=1} }
  ok {print}'

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can try something like this:

sed -r -n '/^[0-9:, -]{24}com\.test\.data /{:a;n;/^[0-9:, -]{24}/!ba;/ com\.test\.data /ba;};p;' file

In some unix systems, the option -r doesn't exist and is replaced by -E (to use the extended regular expression syntax)

Upvotes: 1

Claes Wikner
Claes Wikner

Reputation: 1517

Prints lines with Testing 123

awk '/Testing 123/' file
2016-01-05 14:46:05,264 com.test.rest - Testing 123
2016-01-05 14:46:05,264 com.test.rest - Testing 123

Upvotes: 0

Benjamin W.
Benjamin W.

Reputation: 52152

This script loops through the input and remembers if the last line that started with a date contained com.test.data or not. If it didn't, the line gets printed.

#!/bin/bash

re='^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'

printflag=1

while IFS= read -r line || [[ -n $line]]; do
    if [[ $line =~ $re ]]; then
        if [[ $line =~ com\.test.\data ]]; then
            printflag=0
        else
            printflag=1
        fi
    fi
    if (( printflag )); then
        echo "$line"
    fi
done < "$1"

This takes the name of the logfile as parameter. When stored as script, use it like

./script debug.log

Upvotes: 1

Related Questions