Issue executing grep command on Solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue executing grep command on Solaris
# 1  
Old 05-30-2016
Question Issue executing grep command on Solaris

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

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

Output:
Quote:
COL_USER
Command 2:
Code:
grep -m 1 '</name>' jdbc.xml| |sed 's/\(<name>\|<\/name>\)//g'| sed -e 's/^[ \t]*//'

Output:
Quote:
Fast_ds/DataSource
The above works fine with Linux.

But, when i run the same command in Solaris i get
Code:
grep: illegal option -- A
grep: illegal option -- 1
grep: illegal option -- m

Usage: grep -hblcnsviw pattern file . . .

Code:
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.
# 2  
Old 05-30-2016
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
# 3  
Old 05-30-2016
Quote:
Originally Posted by Corona688
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
Code:
/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?

Last edited by mohtashims; 05-30-2016 at 07:28 PM..
# 4  
Old 05-30-2016
If you don't want to install GNU grep, a crude simulacrum can be made in awk.
Code:
$ 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

$

# 5  
Old 05-30-2016
There is an extraneous pipe symbol in your second Linux command line:
Code:
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:
Code:
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.
# 6  
Old 05-31-2016
Hi.

Some alternatives to GNU grep that allow -A, -B:
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with awk command between Linux and Solaris

Hi, Here is the output using bash profile on Linux uptime 04:59:14 up 16 days, 4:48, 2 users, load average: 1.00, 1.00, 1.20 Here is the output using bash profile on Solaris uptime 4:00am up 84 day(s), 22:21, 3 users, load average: 0.09, 0.10, 0.12 Now,... (4 Replies)
Discussion started by: mohtashims
4 Replies

2. Shell Programming and Scripting

Issue using empty variable in grep command

Hi, I'm writing a shell script and trying to grep a variable value, it works fine as long as there is a value in /tmp/list.out which is captured in $DSK but sometimes the file tends to be empty and that is where I'm having an issue while using grep which returns nothing. I know I can use something... (15 Replies)
Discussion started by: mbak
15 Replies

3. Shell Programming and Scripting

Issue when executing shc command

Hi All, I am trying to create an executable using shc. I have a script which is creating 10 executables. The executables being created have the below issue. The message "Please contact your provider" is displayed even though I am not using the “-e” option. This message is coming for some... (5 Replies)
Discussion started by: temp_user
5 Replies

4. UNIX for Dummies Questions & Answers

grep issue (Solaris)

Can anyone explain this (i.e. why the 2nd grep does not find anything)?: -bash-3.00$ cat tmp.log sftp> chdir /home/test-dir sftp> mget thosefiles*.txt File "/home/test-dir/thosefiles*.txt" not found. -bash-3.00$ grep "s*.txt" tmp.log sftp> mget thosefiles*.txt File... (4 Replies)
Discussion started by: dhebden
4 Replies

5. Shell Programming and Scripting

Issue while executing script

Hi, I'm trying to use the below command in a shell script, but couldn't. The command is working fine when I'm executing from the dollar prompt i.e. shell. Command: grep -i fail /home/applmgr/error.log |egrep -i "`date --date="-4 day" +"%m/%d/%Y"`|`date --date="-4 day" +"%m/%d/%y"`"... (8 Replies)
Discussion started by: venkatesh17
8 Replies

6. Shell Programming and Scripting

CUT Command and grep issue

I am trying to grep the oracle erros evry day from the logs file. My problem is : -rw-r----- 1 tibcolm tibco 17438361 Apr 5 11:59 RetryService-RetryService.log -rw-r----- 1 tibcolm tibco 245303 Apr 5 12:00 ResponseService-ResponseService.log -rw-r----- 1 tibcolm tibco 2122654 Apr 5 12:00... (4 Replies)
Discussion started by: neeraj617
4 Replies

7. UNIX for Dummies Questions & Answers

Grep command issue

How am I supposed to print with one Unix command the names (and only the names) of company JMT? Problems with scandinavian letters? By typing grep JMT url | egrep --only-matching ']+'I still have company name and + marks I should get rid of. I'd be very grateful of your help bacause I am new... (1 Reply)
Discussion started by: jht
1 Replies

8. HP-UX

Performance issue with 'grep' command for huge file size

I have 2 files; one file (say, details.txt) contains the details of employees and another file (say, emp.txt) has some selected employee names. I am extracting employee details from details.txt by using emp.txt and the corresponding code is: while read line do emp_name=`echo $line` grep -e... (7 Replies)
Discussion started by: arb_1984
7 Replies

9. Shell Programming and Scripting

issue with grep command

i am using 'psu' to grep the status of a process like below :- psu|grep -i agentf 29559 29552 0 00:53:05 ? 0:57 gn1avm_agent -n AGENTF1 28946 1 0 00:51:31 ? 0:31 gnavm_ewd -b gn1avm_agent -n AGENTF1 -c -n AGENTF1 29552 28946 0 00:53:05 ? 0:00 /bin/ksh... (6 Replies)
Discussion started by: deepakiniimt
6 Replies

10. Solaris

Troubles building on solaris 9 and executing on solaris 10

Hi, I have to build an application on solaris 9 which is supposed to run on solaris 10 computers. I heard that solaris 9 builds should run on solaris 10 without any troubles. However, there is some trouble. Calling ldd on the programm reveals that "libgcc_s.so.1" and "libstdc++.so.6" are... (4 Replies)
Discussion started by: mikehammer80
4 Replies
Login or Register to Ask a Question