Skip to main content
4 of 7
added 287 characters in body
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

Just do:

#!/bin/sh -

{
  printf '%s\n' "${1-default-id}"
  awk '/abc/ && /def/ && ! /ghi/'
} | socat - tcp:n.n.n.n:7777

${1-default-id} expands to the first positional parameter if specified or default-id otherwise. Replace with ${1?} to exit with an error if not passed any argument instead (or ${1?The error message} to specify an error message instead of the default).

Or to only print the ID if and when one line has been read and matches:

#!/bin/sh -
ID=${1-default-id} awk '
  /abc/ && /def/ && ! /ghi/ {
    if (!already_printed++) print ENVIRON["ID"]
    print
  }' | socat - tcp:n.n.n.n:7777

Or to prepend the ID (and a space character) to every line:

#!/bin/sh -
ID=${1-default-id} awk '
  /abc/ && /def/ && ! /ghi/ {
    print ENVIRON["ID"], $0
  }' | socat - tcp:n.n.n.n:7777
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k