how to grep a log and print multiple string or text from a log - unix

ERROR|2017-04-04 06:27:20|ID=15098|ST=2018-04-0406:27:21|TYPE=Log|LOG=6|OBJECT=NoticeBService|T_TIME=10|REQUEST_MSG=<Envelope><Header><ns11:messageId>184745460</ns11:messageId><ns11:messageDateTime>2018-04-13T11:27:21Z</ns11:messageDateTime></Header></Envelope>|RESPONSE_MSG=<Envelope><Header><m:messageId>184460</m:messageId><m:messageDateTimeStamp>2018-04-04T06:27:21-05:00</m:messageDateTimeStamp></m:trackingMessageHeader></m:wsMessageHeader></Header><Body><Fault><faultcode>Server.704</faultcode><detail><ns1:providerError><ns1:providerErrorCode>704</ns1:providerErrorCode><ns1:providerErrorText>business_rule_exception-Server.704: 'Active'status.</ns1:providerErrorText></ns1:providerError></detail></Fault></Body></Envelope>
I want to print from a test.log:
OBJECT=NoticeBService
business_rule_exception-Server.704: 'Active'status.
I used : sed -n '/providerErrorText/,/providerErrorText/p' | cut -d '|' -f 7 test.log
I getting output :
OBJECT=NoticeBService
sed: -e expression #1, char 31: extra characters after command

with grep
$ grep -oP 'OBJECT[^|]+|(?<=providerErrorText>)[^<]+' file
OBJECT=NoticeBService
business_rule_exception-Server.704: 'Active'status.
Explanation
OBJECT[^|]+ looking for the literal "OBJECT" and match until a pipe symbol
(?<=providerErrorText>) look-behind, find the pattern but not capture
[^<]+ capture everything until the < sign
-oP o is for outputting only the matched pattern, P for perl compatibility (for look-behind matching here).
pattern1|pattern2 is for either pattern1 or pattern2 match (it can be both).

Related

Match exact string and returns only that line in UNIX

I'm looking to match only a particular string on a file and returns only that line.
for eg, my matching pattern is /gp/digital/fiona/payment-checkout if I grep this one I'm getting all 4 lines as below,
/gp/digital/fiona/payment-checkout/img/logo.svg<br>
/gp/digital/fiona/payment-checkout/undefined<br>
/gp/digital/fiona/payment-checkout/uedata/nvp/unsticky<br>
/gp/digital/fiona/payment-checkout
But I want only the line which contains /gp/digital/fiona/payment-checkout.
I tried grep -o -P -w everything.
Using grep:
$ grep -x /gp/digital/fiona/payment-checkout file
/gp/digital/fiona/payment-checkout
man grep:
-x, --line-regexp
Select only those matches that exactly match the whole line. For
a regular expression pattern, this is like parenthesizing the
pattern and then surrounding it with ^ and $
Using awk:
$ awk '$0=="/gp/digital/fiona/payment-checkout"' file
/gp/digital/fiona/payment-checkout

how to grep nth string

How to use "grep" shell command to show specific word from a line starting with a specific word.
Ex:
I want to print a string "myFTPpath/folderName/" from the line starting with searchStr in the below mentioned line.
searchStr:somestring:myFTPpath/folderName/:somestring
Something like this with awk:
awk -F: '/^searchStr/{print $3}' File
From all the lines starting with searchStr, print the 3rd field (field seperator set as :)
Sample:
AMD$ cat File
someStr:somestring:myFTPpath/folderName/:somestring
someStr:somestring:myFTPpath/folderName/:somestring
searchStr:somestring:myFTPpath/folderName/:somestring
someStr:somestring:myFTPpath/folderName/:somestring
AMD$ awk -F: '/^searchStr/{print $3}' File
myFTPpath/folderName/
Remember that grep isn't the only tool that can usefully do searches.
In this particular case, where the lines are naturally broken into fields, awk is probably the best solution, as #A.M.D's answer suggests.
For more general case edits, however, remember sed's -n option, which suppresses printing out a line after edits:
sed -n 's/searchStr:[^:]*:\([^:]*\):.*/\1/p' input-file
The -n suppresses automatic printing of the line, and the trailing /p flag explicitly prints out lines on which there is a substitution.
This matching pattern is fiddly – use awk in this fielded case – but don't forget sed -n.
You could get the desired output with grep itself but you need to enable -P and -o parameters.
$ echo 'searchStr:somestring:myFTPpath/folderName/:somestring' | grep -oP '^searchStr:[^:]*:\K[^:]*'
myFTPpath/folderName/
\K discards the characters which are matched previously from printing at the final leaving only the characters which are matched by the pattern exists next to \K. Here we used \K instead of a variable length positive lookbehind assertion.

How to use grep to get special character

I want to grep \" as following text in file(abc) like:
$egrep -n "^\"$" abc
"CO_FA_SC_600212","2","\"HSE 48\" 48 CHIVALRY AVE"
But its not appearing how could i use egrep or grep to get the line.
grep -F 'special char' filename will search the lines which has special characters.
grep -Fn 'special char' filename gets the line number too.
man grep says,
-F, --fixed-strings: Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched
Here are two alternatives:
grep -n "\\\\\"" filename
grep -n '\\"' filename
For the first one, two consecutive \\ act as a single \, and " was escaped by \, so \\" is passed to grep.
For the second one, \ is taken literally, so \\" is passed to grep
3 and 4 can be verified by echo "\\\\\"" and echo '\\"'

grep for special characters in Unix

I have a log file (application.log) which might contain the following string of normal & special characters on multiple lines:
*^%Q&$*&^#$&*!^#$*&^&^*&^&
I want to search for the line number(s) which contains this special character string.
grep '*^%Q&$*&^#$&*!^#$*&^&^*&^&' application.log
The above command doesn't return any results.
What would be the correct syntax to get the line numbers?
Tell grep to treat your input as fixed string using -F option.
grep -F '*^%Q&$*&^#$&*!^#$*&^&^*&^&' application.log
Option -n is required to get the line number,
grep -Fn '*^%Q&$*&^#$&*!^#$*&^&^*&^&' application.log
The one that worked for me is:
grep -e '->'
The -e means that the next argument is the pattern, and won't be interpreted as an argument.
From: http://www.linuxquestions.org/questions/programming-9/how-to-grep-for-string-769460/
A related note
To grep for carriage return, namely the \r character, or 0x0d, we can do this:
grep -F $'\r' application.log
Alternatively, use printf, or echo, for POSIX compatibility
grep -F "$(printf '\r')" application.log
And we can use hexdump, or less to see the result:
$ printf "a\rb" | grep -F $'\r' | hexdump -c
0000000 a \r b \n
Regarding the use of $'\r' and other supported characters, see Bash Manual > ANSI-C Quoting:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard
grep -n "\*\^\%\Q\&\$\&\^\#\$\&\!\^\#\$\&\^\&\^\&\^\&" test.log
1:*^%Q&$&^#$&!^#$&^&^&^&
8:*^%Q&$&^#$&!^#$&^&^&^&
14:*^%Q&$&^#$&!^#$&^&^&^&
You could try removing any alphanumeric characters and space. And then use -n will give you the line number. Try following:
grep -vn "^[a-zA-Z0-9 ]*$" application.log
Try vi with the -b option, this will show special end of line characters
(I typically use it to see windows line endings in a txt file on a unix OS)
But if you want a scripted solution obviously vi wont work so you can try the -f or -e options with grep and pipe the result into sed or awk.
From grep man page:
Matcher Selection
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified
by POSIX.)

unexpected EOF while looking for matching `"'

I am writing a script which has command to execute as below:
cat /abc | grep -v ^# | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
When running the script on SunOS, i am getting below error:
test: line 1: unexpected EOF while looking for matching `"'
test: line 3: syntax error: unexpected end of file
Tried with different option.. but no luck.
Need somebody help me identify what is missing in the above command.
what are those escapes ?!
cat /abc | grep -v '^#' | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
^ ^ ^ ^
Your problem is there:
sed -e '\''s/"//g'\''
^ unmatched
The quoting is all wrong. Why do you use single quote, backslash, single quote, single quote,and always in that order? Regardless, you have an unquoted double quote, so the shell expects you to add a closing quote for the quoted string which starts with that opening double quote.
As a matter of style, you should also lose the Useless Use of Cat, and think about how to simplify your script. At least:
grep -v ^# /abc | grep -i root | sed -e 's/"//g' | awk '{print $2}'
... but in practice
awk '/^#/ { next } /[Rr][Oo][Oo][Tt]/ { gsub ("\"",""); print $2 }' /abc
Because some of the characters in the awk and sed scripts have a special meaning to the shell, we put them in single quotes. If you need to have single quotes in a script, you need to double quote them; a frequent pattern is to have a string in single quotes adjacent to a string in double quotes, like this: echo '"'"'". This echos " (quoted in single quotes) immediately followed by ' (quoted in double quotes).
Edit Updated analysis of quoting problem; added code example; corrected code example. Final edit corrects quoting of gsub in awk script, and adds a small discussion of quoting.

Resources