# Aix grep lines before and after match

**URL:** <https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340>\
**Category:** AIX\
**Created:** [July 19, 2021, 9:24pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340 "2021-07-19T21:24:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [July 19, 2021, 9:24pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/1 "2021-07-19T21:24:55Z")

</div>

I am trying to grep for 5 lines before and after a match. Since I am using AIX I do not have the GNU feature of :

```
grep -B 5 -A 5 pattern file

```

Is there a way to do this with grep, awk, perl, sed, something else? Can I please get a one liner? I am trying to do this across multiple servers.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [July 20, 2021, 2:06am UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/2 "2021-07-20T02:06:23Z")

</div>

@cokedude, any luck searching these forums for similar topics? It's been covered numerous times...

 ![image](https://community.unix.com/uploads/default/original/3X/d/5/d5abab85648181b79aab570406b4b47ce466531f.jpeg)

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [July 20, 2021, 9:17pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/6 "2021-07-20T21:17:56Z")

</div>

An attempt with awk:

```
awk 'del=(a&&a--) {print; da=!a} $0~pattern{if (da) {print "--"; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=$0}' B=5 A=2 pattern=test filename

```

to emulate

```
grep -B5 -A2 test filename

```

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [July 21, 2021, 3:47pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/7 "2021-07-21T15:47:56Z")

</div>

Hi.

A few of the suggestions in [Grep -C option not available - #4 by CarloM](https://community.unix.com/t/grep-c-option-not-available/348619/4) will allow a single command line to emulate GNU `grep`, but only after you install the alternate grep-work-alike codes, such as `peg` or `cgrep`.

Best wishes ... cheers, drl

---

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [August 3, 2021, 7:19pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/8 "2021-08-03T19:19:36Z")

</div>

This works when running a command on the current server. I have noticed when I try to run this on a remote server it does not work.

```
ssh server "awk 'del=(a&&a--) {print; da=! a} $0~pattern{if (da) {print "--"; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=$0}' B=5 A=2 pattern=Successful file"
bash: !a}: event not found

```

I tried to backslash it.

```
ssh server "awk 'del=(a&&a--) {print; da=\!a} $0~pattern{if (da) {print "--"; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=$0}' B=5 A=2 pattern=Successful file"

syntax error The source line is 1.
 The error context is
                del=(a&&a--) {print; >>> da=\ <<< !a} bash~pattern{if (da) {print --; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=bash}
 awk: The statement cannot be correctly parsed.
 The source line is 1.

```

I tried set +H like this site suggested.

```
ssh server "set +H; awk 'del=(a&&a--) {print; da=! a} $0~pattern{if (da) {print "--"; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=$0}' B=5 A=2 pattern=Successful file"
ksh: +H: bad option(s)

```

Is there a way to get this to run on a remote server?

---

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [August 3, 2021, 7:20pm UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/9 "2021-08-03T19:20:49Z")

</div>

I have been unable to convince the senior admin to install this.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [August 4, 2021, 8:38am UTC](https://community.unix.com/t/aix-grep-lines-before-and-after-match/383340/10 "2021-08-04T08:38:47Z")

</div>

`ssh host command args` can give you a quoting headache, because there is the local ssh command where the local shell does a evaluation/dequoting, and then the remote command where the remote shell does another evaluation/dequoting.  
Here comes a solution.  
Save the command to a file, for example mygrep.sh in the current directory. Let the script pass the script arguments:

```
#!/bin/sh
awk 'del=(a&&a--) {print; da=!a} $0~pattern{if (da) {print "--"; da=0} a=A; if (B) {for(i=NR; i<B+NR; i++) if((i%B) in b) print b[i%B]} {print; da=1}} (B) {if (del) delete b[NR%B]; else b[NR%B]=$0}' "$@"

```

Then run the ssh as follows:

```
ssh host /bin/sh -s B=5 A=2 pattern=Successful file < mygrep.sh

```

The ssh remote shell runs another /bin/sh where -s tells to read commands from stdin and take the following arguments.  
The stdin comes from the local mygrep.sh file.
