Zoltán Veres
Zoltán Veres

Reputation: 21

Reading from log then deleting matching data in another file

I want to write a script using Oscam Casrdsharing server

this is my test:

cat oscam.log | grep "error"

sample output:

2018/10/17 16:43:07 5C94A12E p    (cccam) cccam(r) test.dyndns.org: login failed, error

Once I've found an error, I need to remove its information inside another file :

oscam.server

[reader]
label                         = test.dyndns.org
protocol                      = cccam
device                        = test.dyndns.org,12000
key                           = 0102030405060708091011121314
user                          = renegade
password                      = renegade123
inactivitytimeout             = 30
group                         = 1
cccversion                    = 2.1.2
cccmaxhops                    = 0
cccmindown                    = 1
ccckeepalive                  = 1

blalbal
blalblalb
blalblalb
lablalb

I need to delete those "test.dyndns.org" lines that matched. I would like to keep only the following :

blalbal
blalblalb
blalblalb
lablalb

So far, I've tried the following code :

awk '/test.dyndns.org/{while(getline && $1 != ""){}}1' oscam.server

#output is :
[reader]

blalbal
blalblalb
blalblalb
lablalb

But the line [reader] is still present. What could I do to remove the block entirely?

Upvotes: 0

Views: 335

Answers (3)

Zoltán Veres
Zoltán Veres

Reputation: 21

Looks like this is an easier way, only NAME parameter needs to be focused in the script now.

http://<IP>:<PORT>/oscamapi.html?part=readerlist&label=<NAME>&action=delete

This can be done without using perl.

Upvotes: 0

Zolt&#225;n Veres
Zolt&#225;n Veres

Reputation: 21

I made now finally a prompt method :), anyway dint want editing in-edit so need touch mv parameter after that

#!/bin/bash
PHONEFILE=/etc/tuxbox/config/oscam_modern/oscam.server
PHONEFILE2=/etc/tuxbox/config/oscam_modern/oscam.server.new 


read -p "Delete server (paste the url that was listed on top): " oldnum
if [ ! "$oldnum" ]; then exit; fi
perl -sp0e 's/\[reader\]((?!\n\n).)*\Q$input\E((?!\n\n).)*((\n\n)|(\n?\Z))//sig' -- -input="$oldnum" $PHONEFILE > $PHONEFILE2
mv $PHONEFILE2 $PHONEFILE

Upvotes: 0

Aserre
Aserre

Reputation: 5062

For your problematic, you are looking for a multi-line pattern that :

  • begins by [reader]
  • contains test.dyndns.org
  • is followed by an empty line, i.e. 2 newline characters

You could use perl to do that :

perl -p0e 's/\[reader\]((?!\n\n).)*test\.dyndns\.org((?!\n\n).)*((\n\n)|(\n?\Z))//sig' oscam.server

perl 's/.../.../s' is a command to substitute the matched regex by another pattern, and the ig flags are used to removed every occurrences in your text input. Here, we replace the found pattern by an empty string.

The -p0e flags are used to enable multi-line processing for perl. You can use -p0i.bak -e for in-place file edition (be careful : it will permanently change your input file.)

., ] and [ are escaped in the command because they have a specific meaning for regexes.

Regex explanation :

  • \[reader\] : A pattern that begins by the "[reader]" string ...
  • ((?!\n\n).)* : negative lookaround. Followed by any characters that don't form 2 newlines ...
  • test\.dyndns\.org : Followed by the string "test.dyndns.org" ...
  • ((?!\n\n).)* : Followed by any characters that don't form 2 newlines ...
  • ((\n\n)|(\n?\Z)) : That ends by 2 newlines OR the end of the file

Edit:

To use a bash variable in a perl script, you can use the -s flag.

In order to automatically quote the content of a perl variable, you'll have to use the perl special characters : \Q and \E.

Your function will now look like this :

perl -sp0e 's/\[reader\]((?!\n\n).)*\Q$input\E((?!\n\n).)*((\n\n)|(\n?\Z))//sig' -- -input="<DESIRED INPUT HERE>" oscam.server

Upvotes: 1

Related Questions