For example if I want to print in file drivers/pci/controller/dwc/pci-meson.c, the lines starting with dw_pcie_ops.*=
and ending with ^}
, I can do
$ awk '/dw_pcie_ops.*=/{inblk=1} inblk==1&&/^}/{print $0; inblk=0} inblk==1{print $0}' drivers/pci/controller/dwc/pci-meson.c
static const struct dw_pcie_ops dw_pcie_ops = {
.link_up = meson_pcie_link_up,
.start_link = meson_pcie_start_link,
};
You see the expected result above. I occasionally use this type of command with for each statement and with file name prints like ### filename ###
marks and another awk command in pipeline to remove the filename-only lines.
Now because I use it frequently, I thought maybe I can define a function in bash for doing this(finding lines enclosed by two regular expressions in a file). So I tried
# usage : patinpat pat1 pat2 filename
function patinpat ( ) {
echo 'running patinpat'
echo '$1 = ' $1
echo '$2 = ' $2
echo '$3 = ' $3
awk '/"$1"/{inblk=1} inblk==1&&/"$2"/{print $0; inblk=0} inblk==1{print $0}' "$3"
}
But when I do
$ patinpat dw_pcie_ops.*= ^} drivers/pci/controller/dwc/pci-meson.c
running patinpat
$1 = dw_pcie_ops.*=
$2 = ^}
$3 = drivers/pci/controller/dwc/pci-meson.c
I can see the function arguments were passed ok, but because the awk command also uses the words in the line as $0, $1, $2, .. it cannot differentiate the bash function argument from the words in the line. How can I do this??
ADD : For test, let's just say file drivers/pci/controller/dwc/pci-meson.c 's content is :
static int meson_pcie_host_init(struct pcie_port *pp)
{
struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
struct meson_pcie *mp = to_meson_pcie(pci);
pp->bridge->ops = &meson_pci_ops;
meson_set_max_payload(mp, MAX_PAYLOAD_SIZE);
meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE);
return 0;
}
static const struct dw_pcie_host_ops meson_pcie_host_ops = {
.host_init = meson_pcie_host_init,
};
static const struct dw_pcie_ops dw_pcie_ops = {
.link_up = meson_pcie_link_up,
.start_link = meson_pcie_start_link,
};