0

I have large data file, like this:

AM_fep=1;AF=8.236e-06;AN=121412…
AM_fep =1; AN=121412;AF=0.000265…
AM_fep =2;AF=1.647e-05;AN=121412…

I require to grep only AF= field with its numeric value.
I have used grep -o -E 'AF=[0-9]*' but it gives the first digit of the value as:

AF=8
AF=0
AF=1

2 Answers 2

1

An AWK Solution

Given your corpus, you can field-split on semicolons and then loop over each field on the line looking for AF as a match. For example:

$ awk -F\; '{for (i=1; i<=NF; i++) if ($i ~ /AF/) print $i}' /tmp/corpus 
AF=8.236e-06
AF=0.000265…
AF=1.647e-05

Note that the second example correctly matched the ellipsis character contained in your posted corpus. Your real data probably doesn't have them, but if it does you can use the match() or sub() string functions to exclude it. As an example:

$ awk -F\; '{for (i=1; i<=NF; i++) if ($i ~ /AF/) {sub(/…/, "", $i); print $i}}' \
  /tmp/corpus
AF=8.236e-06
AF=0.000265
AF=1.647e-05
Sign up to request clarification or add additional context in comments.

Comments

0

This requires using grep's -o option which only prints the part of the line that matched the pattern.

We can match this with

grep -o 'AF=[^;]*' datafile

The pattern matches the literal string AF= followed by any number of non-semicolon characters. Against your file, it produces

AF=8.236e-06
AF=0.000265…
AF=1.647e-05

Using egrep (or equivalently grep -E) we can require that there be at least one character after the equal sign with

egrep -o 'AF=[^;]+' datafile

The reason that your attempted pattern is not working is that it matches AF= followed by any number of digits. Thus it can not pick up the decimal point (or the exponent symbol and negative sign).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.