solaris redirection


 
Thread Tools Search this Thread
Operating Systems Solaris solaris redirection
# 1  
Old 06-03-2011
solaris redirection

Hi

I am using solaris 10. When running a grep command with multiple files the output is the same as the order of the input. As soon as I pipe the output to another command then it seems that standard error takes precedence, over standard output and gets sent to the pipe first.

ie grep -c 'findme' file1 nofile file2

would give an output in order:
file1:0
grep: cant open nofile
file2:0

However if I do a grep -c 'findme' file1 nofile file2 | cat

I get
grep: cant open nofile
file1:0
file2:0

using grep -c 'findme' file1 nofile file2 2>&1 | cat

I am trying to make it work so that the output order stays the same as the input order, the above works in linux but in solaris stderr still outputs first. Can anyone help me with this.

thanks
# 2  
Old 06-03-2011
You first need to define what you mean by "output order".

Stdout and stderr are two distinct streams of data. In general there is no concept of order between the two.
# 3  
Old 06-03-2011
Quote:
Originally Posted by chronics
I am using solaris 10. When running a grep command with multiple files the output is the same as the order of the input. As soon as I pipe the output to another command then it seems that standard error takes precedence, over standard output and gets sent to the pipe first.
That's by design. stdout is buffered...a lot of data is collected in a large array and when it's full a single write occurs. This is for efficiency... the write system call is expensive. On the other hand stderr is only line buffered...as soon as a line is available it is displayed. The idea is that stderr should be rarely used, but when it is used it must be displayed immediately.

The only portable way to solve your problem is to write a script that loops over the input files and invokes grep once for each file.

---edit---
Well I guess it's not the only way... you could use expect for instance. There's always another way to skin a cat.
# 4  
Old 06-03-2011
Code:
grep -c 'findme' file1 nofile file2  2>&1 | cat

should (and does for me) preserve order on Solaris too. What shell are you using on what Solaris release ?
# 5  
Old 06-03-2011
The shell is irrelevant but I will try ksh, sh, and bash...
Code:
$ uname -a
SunOS geo-support1 5.10 Generic_127111-10 sun4u sparc SUNW,Sun-Blade-1000
$ ksh
$ grep -c 'findme' file1 nofile file1 2>&1
file1:0
grep: can't open nofile
file1:0
$ grep -c 'findme' file1 nofile file1 2>&1 | cat
grep: can't open nofile
file1:0
file1:0
$
$
$ ^D
$ sh
$ grep -c 'findme' file1 nofile file1 2>&1 | cat
grep: can't open nofile
file1:0
file1:0
$ $
$ bash
bash-3.00$ grep -c 'findme' file1 nofile file1 2>&1 | cat
grep: can't open nofile
file1:0
file1:0
bash-3.00$

The buffering I talked about is internal to grep. Switching shells has no influence. If it works for you, you must be using a different grep. The behavior of your grep is not guaranteed by any standard and it is not wise to write scripts that depend on it.
# 6  
Old 06-03-2011
Thanks for the responses.

SunOS 5.10 - Bash - It does seem to have obsolete utils though such as a less that does not seem to know what is a real newline as opposed to a newline on the console window. It looks as though i will need to use a loop, Perderabo explanation makes sense, does seem strange though that 2>&1 would work on some systems and not others.

Didnt see the above response, as you say it must be the version of grep which incidentally does correctly recognise a newline.
# 7  
Old 06-04-2011
You are right, the shell is not to blame here. I was fooled by Solaris 11 Express default PATH having GNU tools first.
On Solaris 10, to get the expected behavior, you can use:
Code:
/usr/sfw/bin/ggrep -c 'findme' file1 nofile file1 2>&1 | cat

The root cause is stdout is line buffered when the output is a terminal but block buffered when it goes to a pipe or a file. stderr is always line buffered.
Gnu grep is also block buffering stdout in the latter case, but it flushes its standard output stream between each processed file.

---------- Post updated at 14:38 ---------- Previous update was at 11:20 ----------

Quote:
Originally Posted by chronics
less that does not seem to know what is a real newline as opposed to a newline on the console window.
...
grep which incidentally does correctly recognise a newline.
This is unrelated. You might want to start a new thread and elaborate a little bit about these newlines issues.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Redirection does not work in Solaris

Hi all, i have been trying to direct o/p of one command to file, but i don get any entries in file but ouptput get displayed on command prompt. I have tried many options but still , it does not work. please guide. here is the command -bash-3.00$ /usr/local/bin/sudo lpstat -p | grep... (2 Replies)
Discussion started by: manalisharmabe
2 Replies

2. UNIX for Dummies Questions & Answers

about different redirection

explain the redirections 1>, 2>, 3>, ..... and 1< ,2<,3<..... where we use these things thanks Thread moved from AIX forum (2 Replies)
Discussion started by: tsurendra
2 Replies

3. UNIX for Dummies Questions & Answers

echo and redirection

I am trying to write a simple script which prints some output, but also saves the same in a logfile. echo hello echo hello >> logfile Is there anyway i Can write single sentence of code and get the same result: ( i am using CSH ) (4 Replies)
Discussion started by: animesharma
4 Replies

4. Shell Programming and Scripting

Redirection

Hello All, I am using the below script to gather various tools running by the user, we have more than 100 tools running on the server so my challenge is to redirect memory & cpu load to the file with the name of the tool.so am using the below script i am stucking how to redirect to the file... (2 Replies)
Discussion started by: ajaincv
2 Replies

5. Shell Programming and Scripting

I/O redirection

Hello everyone,I'm reading a book and there's code fragment: exec 3>&1 ls -l 2>&1 >&3 3>&- | grep bad 3>&- exec 3>&- It says that the red part of that code does not close fd 3 but the green does close the fd 3.I can't understand that.....Why?Any predicate will be appreciated.:) (18 Replies)
Discussion started by: homeboy
18 Replies

6. UNIX for Dummies Questions & Answers

Help with Redirection

Hi Guys, I m new to UNIX and new to this forum. Was wondering if someone can help me understand redirection (standard input output pipeline etc) for starters, not too sure what this would mean who | sort > sortedfile | pr | lp im starting to understand common commands but when throwing... (2 Replies)
Discussion started by: jmack123
2 Replies

7. Shell Programming and Scripting

redirection

Hi, The code below works, it's a part of a bash shell script that serve to search a pattern $pattern_da_cercare in the files contained in a directory $directory_iniziale. Now the proble is: How can I redirect stderr to a file? PS: so I want to redirect ALL the errors to a file. I tryed... (9 Replies)
Discussion started by: DNAx86
9 Replies

8. Shell Programming and Scripting

redirection stdin

hello all, I need to create a password change utility for a database. I need to gather at the command line the username, password and database sid. I have the program currently doing this. What I would like to do is not have the new password appear on the screen when I do my read command.... (2 Replies)
Discussion started by: whited05
2 Replies

9. UNIX for Dummies Questions & Answers

stderr redirection

Does anyone know away of redirecting the stderr for a bourne or korn shell script to a file. (5 Replies)
Discussion started by: blakmk
5 Replies

10. Programming

Help with redirection

Here is my problem. I don't know make this redirection thing work. The output file (called output.c) looks like this #include<stdio.h> int main() { int k; int m; print f("%d\n", k); printf("%d\n", m); return 0; } the input file(called input.c) is this #include<stdio.h> int... (2 Replies)
Discussion started by: Shallon1
2 Replies
Login or Register to Ask a Question