ihtus
ihtus

Reputation: 2811

Redirect a filtered by grep STDERR to a file

This article http://burgerbum.com/stderr_pipe.html explains how to grep STDERR, and the filtered result is echoed on the screen.

Example: The archiving program tar produces a lot of error messages on STDERR, some of which are important and some of which can be ignored. We'd like to see only the ones that show that there's really something wrong.

tar -cf /tmp/junk.tar /tmp/real /tmp/fake
tar: Removing leading `/' from member names
tar: /tmp/fake: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors

In order to remove tar: Removing leading '/' from member names and tar: Error exit delayed from previous errors from STDERR, but leave tar: /tmp/fake: Cannot stat: No such file or directory, author suggests the following:

(tar -cf /tmp/junk.tar /tmp/real /tmp/fake 3>&1 1>&2 2>&3 | egrep -v "tar: Removing leading|tar: Error exit delayed from previous errors") 3>&1 1>&2 2>&3

The only STDERR output will be: tar: /tmp/fake: Cannot stat: No such file or directory

Question: how to achieve same results, but instead for echoing => write to a file?

I used tar example because it's native to the article.

But my real case is mysqldump, which generates STDOUT (that I don't want to mess), and also generates STDERR - that I need to filter and write to a file.

mysqldump --user=db_username --password=db_password --add-drop-database --host=db_host db_name 2>> '/srv/www/data_appsrv/logs/mysql_date_ym.log' > '/mnt/backup_srv/PFTLXTKDEV/backup/daily/file_nm_db'

I need to exclude Warning: Using a password string from STDERR that is written to mysql_date_ym.log

I tried variations with 2>> and >, but none works.

Upvotes: 1

Views: 142

Answers (1)

KamilCuk
KamilCuk

Reputation: 141910

In bash, just:

tar ... 2> >(egrep -v ... >&2)

or to file:

tar ... 2> >(egrep -v ... >the_file.txt )

I see no value in switching 1 with 2 descriptors, just use the 3rd descriptor, it's already there.

( tar ... 2>&1 1>&3 | egrep -v ... ) 3>&1 1>&2

Or you seem to want to redirect egrep output to a file. Then do that:

( tar ... 2>&1 1>&3 | egrep ... >thefile.txt ) 3>&1
# or
( tar ... 2>&1 1>&3 | egrep ... ) 3>&1 1>thefile.txt
# or just
( tar ... 2>&1 1>&3 | egrep ... ) 3>&1 >thefile.txt

or with that switching:

( tar ... 3>&1 1>&2 2>&3 | egrep ... ) 3>&1 1>thefile.txt 2>&3
( tar ... 3>&1 1>&2 2>&3 | egrep ... >thefile.txt ) 3>&1 2>&3
# it's just:
( tar ... 3>&1 1>&2 2>&3 | egrep ... >thefile.txt ) 2>&1

Upvotes: 2

Related Questions