0

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?

Aserre
  • 4,397
  • 3
  • 30
  • 51

2 Answers2

1

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
Aserre
  • 4,397
  • 3
  • 30
  • 51
  • how do you format if variable the IP/dyndns address? because on perl reformat the text more difficult for bash integrating – Zoltán Veres Oct 19 '18 at 05:15
  • @ZoltánVeres I edited my answer to address your comment. Let me know if it works for you ! – Aserre Oct 19 '18 at 08:59
  • nothing happened :(, only could on perl? not possible awk, sed, grep method? – Zoltán Veres Oct 20 '18 at 09:09
  • @ZoltánVeres are you sure nothing happened ? Does the command show something ? By default in bash, a command will almost never modify your file. You'll have to use the `-i` flag or use stream redirection. Awk, sed, or other tools will work the same way, but will have a harder time dealing with multilines – Aserre Oct 22 '18 at 08:21
0

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