Pipe psql error output to grep - grep

I'm running a psql query like so:
$ psql --file=foo.sql "BAR-DB"
Where foo.sql contains the query. foo.sql has bad syntax, so the output is a list of error messages. My plan was to pipe the output to grep so I can filter specific error messages, but
$ psql --file=foo.sql "BAR-DB" | grep PATTERN
doesn't seem to do anything. psql's documentation shows the optional flag:
-o, --output=FILENAME send query results to file (or |pipe)
but I'm not sure how to use it to pipe the output to grep. What is the proper syntax?

You could redirect the stderr of psql to stdout and then pipe it to grep:
psql --file=foo.sql "BAR-DB" 2>&1 | grep -- PATTERN

Related

How can i make grep show a line ignoring the words i want?

I am trying to use grep with the pwd command.
So, if i enter pwd, it shows me something like:
/home/hrq/my-project/
But, for purposes of a script i am making, i need to use it with grep, so it only prints what is after hrq/, so i need to hide my home folder always (the /home/hrq/) excerpt, and show only what is onwards (like, in this case, only my-project).
Is it possible?
I tried something like
pwd | grep -ov 'home', since i saw that the "-v" flag would be equivalent to the NOT operator, and combine it with the "-o" only matching flag. But it didn't work.
Given:
$ pwd
/home/foo/tmp
$ echo "$PWD"
/home/foo/tmp
Depending on what it is you really want to do, either of these is probably what you really should be using rather than trying to use grep:
$ basename "$PWD"
tmp
$ echo "${PWD#/home/foo/}"
tmp
Use grep -Po 'hrq/\K.*', for example:
grep -Po 'hrq/\K.*' <<< '/home/hrq/my-project/'
my-project/
Here, grep uses the following options:
-P : Use Perl regexes.
-o : Print the matches only (1 match per line), not the entire lines.
\K : Cause the regex engine to "keep" everything it had matched prior to the \K and not include it in the match. Specifically, ignore the preceding part of the regex when printing the match.
SEE ALSO:
grep manual
perlre - Perl regular expressions

Search through *.gz files with keeping the file name

Say I have multiple .gz files that I want to search a keyword in them. I can do this by piping zcat result to a grep like this:
zcat some.file.* | grep "keyword_1" | ... | grep "keyword_n"
The output of this command though will be just the matching line and won't have the file name in it. Is there any way I can attach the file name to the zcat output?
Try zgrep instead of zcat:
zgrep -H keyword some.file.*
And if you want to use egrep to get pattern matching:
export GREP=egrep
zgrep -H -e "(keyword1|keyword2)" some.file.*

Search a string which contains backslash using Grep command

In the tail output i have following string...Using grep command how can i search for the string "contentState\":\"STOPPED\". I have to search for the whole string "contentState\":\"STOPPED\" rather
than searching for STOPPED or contentState only.
I tried following command: But it is not working.
grep -e ""contentState\":\"STOPPED\" /opt/logs/out.log | tail -1
{\"eventType\":\"appAction\",\"action\":\"CONTENT_STATE_CHANGE\",\"evt\":{\"contentState\":\"STOPPED\"}}}
To search for "contentState\":\"STOPPED\", you need to (a) put the whole string in single-quotes to protect it, and (b) escape (double) the backslashes. Thus:
grep -e '"contentState\\":\\"STOPPED\\"' /opt/logs/out.log
Without the protection provided the outer single-quotes, the unescaped double-quotes would be subject to the shell's quote removal and grep would never see them.
Example
Consider this test file:
$ cat log
good "contentState\":\"STOPPED\"
bad contentState\":\"STOPPED\"
bad "contentState\":\"STOPPED"
Let's run our command:
$ grep -e '"contentState\\":\\"STOPPED\\"' log
good "contentState\":\"STOPPED\"
As we can see, the good line is returned and only the good line.

Can I use grep to show only the matched line, and not the file it appeared in?

I sometimes want to grep for a function to see examples of how it is used in context, eg. what sort of parameters it is called with. When I am doing this, the name of the file the match appears in becomes useless clutter. Is there any way to instruct grep to not include it? (Or a grep alternative that solves the same problem?)
You can tell grep not to indicate the filename in the output with the option -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the
default when there is only one file (or only standard input) to
search.
Test
$ echo "hello" > f1
$ echo "hello man" > f2
$ grep "hello" f*
f1:hello
f2:hello man
$ grep -h "hello" f*
hello
hello man

grep is unable to find all pattern matching "\[\[\[\["

I am having problems with using grep along with a pipe. The scenario is as follows:
I am running a python script that outputs (using print) to the screen debug messages. I use ./prog | grep "\[\[\[\[" to catch the strings with "[[[[" in them. It returns few matching results but not others (Another observation: results found by grep come before the results not found by grep in the file). I have ran the ./prog without pipe and grep and it outputs all the strings with "[[[[" pattern.
The problem is that the left square bracket is a special character in regular expressions. "grep" is not just a string matcher. Regular expressions are an involved language that let you describe patterns of text. Grep is trying to interpret [[[[ as a regular expression, not just a string.
As your question subject suggests, you can usually escape special characters with a backslash. So the following might work:
./prog | grep '\[\[\[\['
You can also "escape" square brackets by putting them inside square brackets. Thus, [[][[][[][[] or [[]{4} if your version of grep handles it.
You also need to determine whether your program, ./prog, is sending output to "standard output" or "standard error". You can put all your stderr through the pipe with:
./proc 2>&1 | egrep '[[]{4}'
UPDATE:
[ghoti#pc ~]$ printf '[[[[\n[[[\n[[[[\n[[[[[\n[[\n' | grep '\[\[\[\['
[[[[
[[[[
[[[[[
[ghoti#pc ~]$ printf '[[[[\n[[[\n[[[[\n[[[[[\n[[\n' | egrep '[[]{4}'
[[[[
[[[[
[[[[[
[ghoti#pc ~]$
Obviously, my results do not match yours. If you can provide more details as to the data you're processing, it will be helpful in trying to duplicate your results.
Error messages are usually sent to stderr, not stdout; your pipe is filtering stdout. (Your "another observation" hints at this.) You can redirect stderr along with stdout to the pipe:
./prog 2>&1 | grep '\[\[\[\['

Resources