This question already has answers here:
Closed 10 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Possible Duplicate:
extract regexp result from string and write it to a variable
Here is my command :
grep -E '\*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile
It outputs :
FIN (SUCCESS)
And I would like it outputs only :
SUCCESS
How can I tell grep to do it ?
You can pipe the output of your grep command to the awk command.
grep -E '*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile | awk '{print $2}'
I am not sure how to do that with grep alone, as it is not really tailored to that exact use case. Since you are on a platform where grep is, use pipes to your advantage when you can have one command solve part of the problem, and another command the other part.
grep is not capable of outputting a single capture group, but you can use sed to do it instead:
sed 's/\*[[:space:]]\+FIN[[:space:]]\+(\([^)]\+\))/\1/g' file
If you use ack then you can use match groups and the --output switch:
ack '\*\s+FIN\((.+?)\)' --output='$1' myfile
Related
This question already has answers here:
grepping output of ps, exclude the word grep [duplicate]
(1 answer)
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
I am running this script docker-maintenance-script.sh. I want to check if the same clone(s) of program(my script) is running on machine. So I am running a ps command inside the script which greps again docker-maintenance-script.sh.
ps -ef | grep -P "(?<!(grep))(docker\-maintenance\-script\.sh)"
output while running the script was:
dxadmin 4497 5231 0 22:32 pts/0 00:00:00 bash -x docker-maintenance-script.sh
which is only infact this current proceess that is greping.
So to the problem -
I want to discard that entry as that is invoker process. I need ps list that only lists other docker-maintenance-script.sh process. not this process.
I need a ps list that discards my current parent pid based on negative look behind usinf pid.
I tried this
ps -ef | grep -P "(?<!(grep))(?<!($$))(docker\-maintenance\-script\.sh)"
($$ returns current pid of process)
in this case 4497. But that is not helping as it 'not match' only when its just right before
(docker-maintenance-script.sh). I want to not match the string whenever
$$ appears anywhere before (docker-maintenance-script.sh). Please Help
I tried too ps -ef | grep -P "(?<!($$.*))(docker\-maintenance\-script\.sh)"
but that returns non-fixed length lookbehind grep error
This question already has answers here:
Regex to extract first 3 words from a string
(3 answers)
Closed 5 years ago.
This seems like it should be simple, but I've spent far too much time searching. How can I use sed and regex to trim off all words in a line after the fourth word?
For instance from:
19900101, This is a title
19091110, This is a really long title
I would like to have
19900101, This is a
19091110, This is a
I've tried answers like this one Regex to extract first 3 words from a string, but I'm using Mac OSX, so I get context address errors.
This is easily done using cut:
cut -d ' ' -f 1-4 file
19900101, This is a
19091110, This is a
Or using awk:
awk '{NF=4} 1' file
19900101, This is a
19091110, This is a
This might work for you (GNU sed):
sed 's/\s*\S*//5g' file
Remove the fifth or more words from the line.
This question already has answers here:
How to grep for the whole word
(7 answers)
Closed 6 years ago.
I'm trying to parse the firewall log file and only take the lines that don't contain the router's address as source. The router's address is the obvious 192.168.2.1 and the computer's address is 192.168.2.110.
If I write grep -v 192.168.2.1 then I don't get the destination 192.168.2.110, because it starts with 192.168.2.1. If I don't use anything, then I get the lines from the router, that I would like to filter out. I have searched and tried different regexs, but no matter what I do, I either get both addresses or none.
This force PATTERN to match only whole words grep -w.
grep -v -w 192.168.2.1 file
192.168.2.110
Or Enclose your pattern with \<pattern\>
grep -v '\<192.168.2.1\>' file
192.168.2.110
You can try to use \b which matches with word boundaries:
grep -vP '\b192.168.2.1\b'
or better yet
grep -vP '\b192\.168\.2\.1\b'
You need the -P mode for this to work.
This question already has answers here:
How do I match multiple addresses in sed?
(4 answers)
Closed 7 years ago.
The following command will remove PATTERN from all the lines 13,14,15...29 containing it:
sed -i 13,29s/PATTERN// file
However, I want to remove PATTERN from only the 13th and 29th line. Obviously, I can use
sed -i 13s/PATTERN//;29s/PATTERN// file
but my pattern is long enough to make this inconvenient so I would like to specify the PATTERN only once. Any ideas? I've tried to search for an answer but found nothing.
Also, is there a valid reason why sed uses a comma instead of dash to match a range of lines? I find it illogical.
Thanks in advance.
use awk:
awk 'NR == 13 || NR == 29 { sub(/PATTERN/, "") } { print }' file
of course, you have to use awk compatible re here. https://www.gnu.org/software/gawk/manual/html_node/Regexp.html#Regexp
the first part achieves your requirement, and the second part just print everything out. you can use redirect to put things in another file, and then move over to the original place.
OK, the following is probably the closest thing to what could possibly be the answer to my question:
sed -i '13,29{14,28!s/PATTERN//}' file
A little longer answer with logical-or:
sed -i '13bA;29bA;b;:A;s/PATTERN//' file
Also, using a variable:
VAR=PATTERN
sed -i "13s/$VAR//;29s/$VAR//" file
Thanks to #Jeff Bowman for redirection, and #HuStmpHrrr for the advice to use a variable.
I run the command grep to get certain output and store it into a variable. The output of grep command is:
5.3.1-6.2011171513.ASMOS6
but I need to only parse out 5.3.1-6 and store that version in a variable. How can I do that?
What about this?
TEXT=5.3.1-6.2011171513.ASMOS6
RES=$(echo $TEXT | cut -d. -f-3)
We cut the string on . and then get the first 3 pieces.
With awk
RES=$(echo $TEXT | awk -F. 'OFS=FS {print $1,$2,$3}')
OFS=FS meaning we get the delimiter to print that was defined with -F.
As you tag says linux, I'll assume that you have a modern grep that supports the -o option.
You can do
var=$(grep -o 'regex' file)
To capture just the regex to a variable.
Unfortunately, I don't understand the problem you're having getting just 5.3.1-6, you have to edit your question to show us the regex youare currently using.
IHTH