grep -v under double quotes query - grep

We have a portion of code which states,
"diff file1 file2 | /usr/bin/grep -v "#" | /usr/bin/grep ^\> | /usr/bin/awk '{print $3}' | /usr/bin/xargs mkdir"
The whole statement is enclosed in double quotes(is a requirement of the application syntax). When the application reaches this stage , it gives the grep error.
This statement works well on the command line. But through application, gives error for grep.
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
So not sure if it is first grep or second grep which is a problem.

Seems like a problem with double quotes. Try changing your first grep to /usr/bin/grep -v '#' and the second grep to /usr/bin/grep '^>'

You are using grep -v ^> and > means "redirect".
If you for example do:
grep ^>output
all the output will be stored in the file output.
So what you need to do is to quote ^> so that it is interpreted as the pattern you are looking for:
"diff file1 file2 | /usr/bin/grep -v "#" | /usr/bin/grep "^>" | /usr/bin/awk '{print $3}' | /usr/bin/xargs mkdir"
^ ^
By the way, note all your greps can be reduced like this:
diff file1 file2 | awk '/#/ || /^>/ {print $3}' | /usr/bin/xargs mkdir
^^^ ^^ ^^^^
either contains # | |
or starts with >

Related

How to grep repeated strings on a single line?

I have this a file.txt with one line, whose content is
/app/jdk/java/bin/java -server -Xms3g -Xmx3g -XX:MaxPermSize=256m -Dweblogic.Name=O2pPod8_mapp_msrv1_1 -Djava.security.policy=/app/Oracle/Middleware/Oracle_Home/wlserver/server/lib/weblogic.policy -Djava.security.egd=file:/dev/./urandom -Dweblogic.ProductionModeEnabled=true -Dweblogic.system.BootIdentityFile=/app/Oracle/Middleware/Oracle_Home/user_projects/domains/O2pPod8_domain/servers/O2pPod8_mapp_msrv1_1/data/nodemanager/boot.properties -Dweblogic.nodemanager.ServiceEnabled=true -Dweblogic.nmservice.RotationEnabled=true -Dweblogic.security.SSL.ignoreHostnameVerification=false -Dweblogic.ReverseDNSAllowed=false -Xms8192m -Xmx8192m -XX:MaxPermSize=2048m -XX:NewSize=1300m -XX:MaxNewSize=1300m -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled
and when I do
cat file.txt | grep -io "Xms.*" | awk '{FS" "; print $1} ' | cut -d "s" -f2
output:
3g
why is grep not reading the second occurrence, i.e. I expect 3g and 8192m.
Infact, how do I print only 8192m in this case?
Your regex just says "find Xms followed by anything repeated 0 to n times". That returns the rest of the row from Xms onward.
What you actually want is something like "find Xms followed by anything until there's a whitespace repeated 0 to n times".
grep -io "Xms[^ ]*" file.txt | awk '{FS" "; print $1} ' | cut -d "s" -f2
In [^ ] the ^ means "not"
I'm not really sure what you are trying to achieve here but if you want the endings of all space-separated strings starting with -Xms, using bare awk is:
$ awk -v RS=" " '/^-Xms/{print substr($0,5)}' file
3g
8192m
Explained:
$ awk -v RS=" " ' # space separated records
/^-Xms/ { # strings starting with -Xms
print substr($0,5) # print starting from 5th position
}' file
If you wanted something else (word repeated in the title puzzles me a bit), please update the question with more detailed requirements.
Edit: I just noticed how do I print only 8192m in this case (that's the repeated maybe). Let's add a counter c and not print the first instance:
$ awk -v RS=" " '/^-Xms/&&++c>1{print substr($0,5)}' file
8192m
You could use grep -io "Xms[0-9]*[a-zA-Z]" instead of grep -io "Xms.*" to match a sequence of digits followed by a single character instead the entire line within a single group:
cat file.txt | grep -io "Xms[0-9]*[a-zA-Z]" | awk '{FS" "; print $1} ' | cut -d "s" -f2
Hope this helps!
The .* in your regexp is matching the rest of the line, you need [^ ]* instead. Look:
$ grep -o 'Xms.*' file
Xms3g -Xmx3g -XX:MaxPermSize=256m -Dweblogic.Name=O2pPod8_mapp_msrv1_1 -Djava.security.policy=/app/Oracle/Middleware/Oracle_Home/wlserver/server/lib/weblogic.policy -Djava.security.egd=file:/dev/./urandom -Dweblogic.ProductionModeEnabled=true -Dweblogic.system.BootIdentityFile=/app/Oracle/Middleware/Oracle_Home/user_projects/domains/O2pPod8_domain/servers/O2pPod8_mapp_msrv1_1/data/nodemanager/boot.properties -Dweblogic.nodemanager.ServiceEnabled=true -Dweblogic.nmservice.RotationEnabled=true -Dweblogic.security.SSL.ignoreHostnameVerification=false -Dweblogic.ReverseDNSAllowed=false -Xms8192m -Xmx8192m -XX:MaxPermSize=2048m -XX:NewSize=1300m -XX:MaxNewSize=1300m -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled
$ grep -o 'Xms[^ ]*' file
Xms3g
Xms8192m
$ grep -o 'Xms[^ ]*' file | cut -d's' -f2
3g
8192m
$ grep -o 'Xms[^ ]*' file | cut -d's' -f2 | tail -1
8192m
or more concisely:
$ sed 's/.*Xms\([^ ]*\).*/\1/' file
8192m
The positive lookbehind of PCRE (the form: (?<=RE1)RE2) can resolve the problem easily:
$ grep -oP '(?<=Xms)\S+' file.txt
3g
8192m
Explains:
-o: show only the part of a line matching PATTERN.
-P: PATTERN is a Perl regular expression.
(?<=Xms)\S+: matches all continuous non-whitespace strings which are just following the string Xms.

How to correctly format df pipe through genmon?

I am having trouble with command output formatting.
In terminal this works nicely:
df | grep sda1 | head -c33 | tail -c7 | tr -d " "
In genmon, I get only numbers such as "1145944":
SDAFREE=$(df | grep sda1 | head -c33 | tail -c7 | tr -d " ")
echo="$SDAFREE"
How do I print that command's output through genmon to xfce panel correctly (same as in terminal)?
Thank you.
I have the same issue with every command with a pipe. As a workaround I put the command in a executable script and run the script in genmon.
BTW:
if you want just one value of a table, you can use awk instead of head, tail and tr:
df | awk '/sda1/ {print $4}'

Cut the Apostrophe (') with cut or awk

I'm trying to cut a line of asterisk output.
The complete line:
[2017-11-01 08:23:58] NOTICE[13443]: res_pjsip/pjsip_distributor.c:659 log_failed_request: Request 'INVITE' from '"66666" <sip:66666#192.168.1.248>' failed for '163.172.107.10:5070' (callid: f64a37f3cc5a88f4cd957ecb7b65a14f) - No matching endpoint found
I need to see only this 163.172.107.10 in my output
So my command is this:
cat test | grep endpoint | awk '{print $13}' | awk -F':' '{print $1}'
My output:
'163.172.107.10
But I can't get rid of the ' (Apostrophe)
I tried: cut -d '''
But that didn't work
Any suggestions?
If this line follows a similar patters as pointed above, then
try this
gawk -F"'" '{print $6}' | cut -d":" -f1
as in
echo "[2017-11-01 08:23:58] NOTICE[13443]: res_pjsip/pjsip_distributor.c:659 log_failed_request: Request 'INVITE' from '"66666" <sip:66666#192.168.1.248>' failed for '163.172.107.10:5070' (callid: f64a37f3cc5a88f4cd957ecb7b65a14f) - No matching endpoint found" | gawk -F"'" '{print $6}' | cut -d":" -f1
Please let me know if the below helps.
cat test | grep endpoint | awk '{print $13}' | awk -F':' '{print $1}' | tr -d "'"
If your Input_file is same as shown sample then following may help you in same.
awk '{sub(/.*failed for \047/,"");sub(/:.*/,"");print}' Input_file

How come I don't get all the lines with grep and grep -v

Why does grep -v POLYGON remove many more lines than those matching grep POLYGON?
$ cat BOUNDARIES3D_LV03.nt | grep -v POLYGON | wc
249 782 137001
$ cat BOUNDARIES3D_LV03.nt | grep POLYGON | wc
2441 2753697 51833677
$ cat BOUNDARIES3D_LV03.nt | wc
73078 2975809 91746795
Is this a bug in grep (using: grep (GNU grep) 2.23) or am I misunderstanding something?
Update
It seems that grep aborts at the first matching line containing an invalid character.
The problem was that grep aborts at the first line containing a byte sequence that doesn't evaluate to a character in the current encoding. The following resolved the issue for me:
export LC_ALL="en_US.UTF-8"

How do i Extract integer value from a string in Unix

when i type this command
/usr/local/afs7/bin/afs_paftools -a about.afs | grep TOTAL_DOCUMENTS
I get a result
TOTAL_DOCUMENTS = 74195
How i can extract the integer number(74195) after =
using grep command
One way is to use grep:
$ echo "TOTAL_DOCUMENTS = 74195" | grep -o '[0-9]\+'
74195
or since you know, that it's the last field, use awk:
$ echo "TOTAL_DOCUMENTS = 74195" | awk '{print $NF}'
74195
or just use awk for the lot:
your-command -a about.afs | awk '/TOTAL_DOCUMENTS/{print $NF}'
If there are no space:
TOTAL_DOCUMENTS=74195
Use this awk
echo "TOTAL_DOCUMENTS=74195" | awk -F= '{print $NF}'
74195

Resources