0

I'm trying to pass a single variable into my sed's search/replace to eliminate duplicate code and make further edits to string easier. Here is my original code:

function dhcplog() {
    cat /var/log/pihole.log | grep --line-buffered dhcp | sed -E '/DHCP, |DHCPACK|DHCPNAK|not giving/a \\'
    tail -f /var/log/pihole.log | grep --line-buffered dhcp | sed -E '/DHCP, |DHCPACK|DHCPNAK|not giving/a \\'
}

and here is the code I'm trying to achieve, by passing the string into both seds with single variable:

function dhcplog() {
    SEQUENCE="/DHCP, |DHCPACK|DHCPNAK|RTR-SOLICIT|not giving|router advertisement on/a \\"
    FILE="/var/log/pihole.log"
    cat $FILE | grep --line-buffered dhcp | sed -E "$SEQUENCE"
    tail -f $FILE | grep --line-buffered dhcp | sed -E "$SEQUENCE"
}

but I always end up with -bash: SEQUENCE: command not found followed by unmodified output (it should place newlines after lines with matches).

edit: after @KamilCuk's fix the output works, but sed refuses to parse it (it should place newlines after lines with matches):

Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPACK(eth0) 192.168.2.168 b0:55:08:05:bb:1a DEVICE
Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPREQUEST(eth0) 192.168.2.168 b0:55:08:05:bb:1a
Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPACK(eth0) 192.168.2.168 b0:55:08:05:bb:1a DEVICE
Aug 11 15:38:34 dnsmasq-dhcp[444]: RTR-SOLICIT(eth0) b0:55:08:05:bb:1a

Desired output (reproduced with old function):

Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPACK(eth0) 192.168.2.168 b0:55:08:05:bb:1a DEVICE

Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPREQUEST(eth0) 192.168.2.168 b0:55:08:05:bb:1a
Aug 11 15:38:33 dnsmasq-dhcp[444]: DHCPACK(eth0) 192.168.2.168 b0:55:08:05:bb:1a DEVICE

Aug 11 15:38:34 dnsmasq-dhcp[444]: RTR-SOLICIT(eth0) b0:55:08:05:bb:1a
jackar
  • 99
  • 1
  • 1
  • 10
  • 1
    `SEQUENCE = "` -> `SEQUENCE="` – KamilCuk Aug 11 '21 at 14:08
  • Thanks, after editing to function dhcplog() { SEQUENCE="/DHCP, |DHCPACK|DHCPNAK|RTR-SOLICIT|not giving|router advertisement on/a \\" FILE="/var/log/pihole.log" cat $FILE | grep --line-buffered dhcp | sed -E $SEQUENCE tail -f $FILE | grep --line-buffered dhcp | sed -E $SEQUENCE } the shell now produces **sed: -e expression #1, char 6: unterminated address regex** error, I presume the file error is fixed and only sed error remains – jackar Aug 11 '21 at 14:13
  • 1
    Quotes. Before writing here, please check your scripts with http://shellcheck.net. Quotes were there before your edit. – KamilCuk Aug 11 '21 at 14:19
  • Thanks, I fixed the file parameter, and sed now accept the variable, but it seems there is still issue with unpacking it to the command, so it either gets ignored or gobbled up. As a result, the newlines after searched phrase arent present – jackar Aug 11 '21 at 14:27
  • 2
    Use single quotes. If you write `seq="a \\"`, seq is assigned a string that contains a single backslash. if you write `seq='a \\'`, you get both backslashes. – William Pursell Aug 11 '21 at 14:33
  • It's not clear what exactly you're trying to do. If you [edit] your question to provide the sample input from which you'd expect to get the output you posted and add a statement of your requirements then we can help you. – Ed Morton Aug 11 '21 at 19:50

2 Answers2

0

After learning a bit about parameter unpacking and thanks to Use sed to replace regex with a variable, I was able to achieve my goal with:

function dhcplog() {
    SEQUENCE='/DHCP, |DHCPACK|DHCPNAK|RTR-SOLICIT|not giving|router advertisement on|no address range available for DHCPv6 request via/a \\'
    FILE="/var/log/pihole.log"
    cat $FILE | grep --line-buffered dhcp | sed -E "${SEQUENCE}"
    tail -f $FILE | grep --line-buffered dhcp | sed -E "${SEQUENCE}"
}

@William Pusell's solution is also valid, and appears tad more elegant.

function dhcplog() {
    SEQUENCE='/DHCP, |DHCPACK|DHCPNAK|RTR-SOLICIT|not giving|router advertisement on|no address range available for DHCPv6 request via/a \\'
    FILE="/var/log/pihole.log"
    cat $FILE | grep --line-buffered dhcp | sed -E "$SEQUENCE"
    tail -f $FILE | grep --line-buffered dhcp | sed -E "$SEQUENCE"
}

Thank you, and feel free to direct me to a source explaining differences between "$SEQUENCE" and "${SEQUENCE}" ^_^

jackar
  • 99
  • 1
  • 1
  • 10
  • There is no difference between `"$SEQUENCE"` and `"${SEQUENCE}"`. There IS a difference between `"$SEQUENCE7"` and `"${SEQUENCE}7"`, and between `"$SEQUENCE[@]"` and `"${SEQUENCE[@]}"`. – Ed Morton Aug 11 '21 at 19:40
  • You should copy/paste your script into http://shellcheck.net and fix the issues it tells you about. For other issues see https://mywiki.wooledge.org/Quotes and [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization). – Ed Morton Aug 11 '21 at 19:44
0

I'm not sure I understand your requirements but it looks like what you're trying to do is this:

extract() {
    awk -v ORS='\n\n' '
        /dhcp.*(DHCP, |DHCPACK|DHCPNAK|RTR-SOLICIT|not giving|router advertisement on)/
    ' "${@:--}"
}

dhcplog() {
    local file='/var/log/pihole.log'
    extract "$file"
    tail -f "$file" | extract
}
Ed Morton
  • 188,023
  • 17
  • 78
  • 185