Issue executing grep command on Solaris

more jdbc.xml
  <name>Fast_ds/DataSource</name>
       <property>
        <name>user</name>
        <value>COL_USER</value>
      </property>

Command 1:

grep -A1 '<name>user</name>' jdbc.xml|grep -v '<name>user</name>'|sed 's/\(<value>\|<\/value>\)//g'| sed -e 's/^[ \t]*//'

Output:

Command 2:

grep -m 1 '</name>' jdbc.xml| |sed 's/\(<name>\|<\/name>\)//g'| sed -e 's/^[ \t]*//'

Output:

The above works fine with Linux.

But, when i run the same command in Solaris i get

grep: illegal option -- A
grep: illegal option -- 1
grep: illegal option -- m

Usage: grep -hblcnsviw pattern file . . .
SunOS mymac 1av 5.10 Generic_150400-23 sun4v sparc sun4v

How can i get output in Solaris same as Linux?

Also solaris equivalent for grep -B1 would be good to know (I will provide a sample in a new thread if it need a sample test case).

Thank You.

These GNU extensions are not found on Solaris' default, standards-compliant grep.

You probably have the GNU version somewhere though, just not used by default. Try /usr/gnu/bin/grep

I do not have any gnu folder under /usr

I tried

/usr/xpg4/bin/grep: illegal option -- A
Usage:  grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
        grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]

Can you please suggest?

If you don't want to install GNU grep, a crude simulacrum can be made in awk.

$ cat bam

1
2
3
4
5
BAM
5
4
3
2
1

$ cat ctx.awk

BEGIN {
        B=0;    A=0;    MAX=100;        LAST=0; P=0
}

function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%MAX]);
}

{ LINE[(++L)%MAX]=$0 } # Remember line for later

($0 ~ PAT) {
        if((NR - LAST) > B)     LAST = (NR-B);

        P=A+1

        while(LAST <= NR)
        {
                print last(NR-LAST);
                LAST++;
        }
        next
}

((--P)>0)

$ nawk -f ctx.awk PAT="BAM" B=1 A=3 bam

5
BAM
5
4
3

$

There is an extraneous pipe symbol in your second Linux command line:

grep -m 1 '</name>' jdbc.xml| |sed 's/\(<name>\|<\/name>\)//g'| sed -e 's/^[ \t]*//'

that would generate a syntax error in shells on both Linux and on Solaris systems.

And, even if Solaris systems had a grep that supported the non-standard options provided as extension on Linux systems, you would also find that the sed commands you're using use extended regular expressions in the substitute commands (when the standards state that basic regular expressions are to be used there, and Solaris sed conforms to the standards and would generate bad BRE syntax errors). But by replacing both the one or two grep commands and both sed commands in your two pipelines with a single awk command as shown below:

printf "Solaris awk replacement for Linux pipeline:
	grep -A1 '<name>user</name>' jdbc.xml|
	grep -v '<name>user</name>'|
	sed 's/\(<value>\|<\/value>\)//g'|
	sed -e 's/^[ \t]*//'
"
/usr/xpg4/bin/awk '
c {	gsub("[[:space:]]|<[/]value>", "")
	print
	c = 0
}
/<name>user<\/name>/ {
	c = 1
	next
}' jdbc.xml

printf "\nSolaris awk replacement for Linux pipeline:
	rep -m 1 '</name>' jdbc.xml|
	sed 's/\(<name>\|<\/name>\)//g'|
	sed -e 's/^[ \t]*//' 
"
/usr/xpg4/bin/awk '
/<\/name>/ {
	gsub("[[:space:]]|<[/]*name>", "")
	print
	exit
}' jdbc.xml

we get code that would run more efficiently and faster on both Linux and Solaris systems.

Hi.

Some alternatives to GNU grep that allow -A, -B:

Print lines above, below pattern-matched line (context, window); "only" string matching pattern
        1) GNU grep -A -B; /usr/sfw/bin/ggrep on some Solaris

        2) peg, a perl script, does not use "-o" like GNU grep;
        allows -A, -B
        http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.10 (or later)

        3) ack, a perl script, can use "-o" like GNU grep; allows -A, -B
           https://metacpan.org/pod/distribution/ack/ack

        4) cgrep does not use "-o" like GNU grep; allows -nline,
        +nline for context like GNU grep -A, -B; matching can cross lines
        http://sourceforge.net/projects/cgrep/

        5) bool, prints context in bytes; matching can cross lines

        6) greppm (xtcgrep), extension of CPAN grep; allow -C context; allows -o;
        complicated calling sequence; http://freecode.com/projects/greppm

        7) sift (aka "gogrep"), extremely fast grep work-alike, allows -A, -B, -C
           can "-o" using "--replace", matching can cross lines
       https://sift-tool.org/ (verified 2015.11.05)

        8) ag, "The Silver Searcher", similar to, but faster than ack.
           In Debian 8.4 repository, as "silversearcher-ag".

        9) pt, "The Platinum Searcher", similar to ag, faster than ack.
           https://github.com/monochromegane/the_platinum_searcher
           Uses "-w" as in "grep "-o"; written in "go". (verified 2016.04.25)

Stand-alone codes can be placed in one's own "bin" directory and used just like system commands (provided the PATH contains that directory).

Best wishes ... cheers, drl