0

How can I extract values from a file using shell-script (grep, awk or sed for example). I have the following structure (described below) and I want to get an output file with only the second column of the file. I tried using grep: grep -oP '\s*U238\s*\*\s+[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?' file >U238_out to extract all U238 values from the entire file and store it in an output file (U238_out).With this I get the following output:

 U238     *  1.779265E+03
 U238     *  5.418484E-03
 U238     *  1.777156E+03
         ...

but I want to obtain this structure:

1.779265E+03
5.418484E-03
1.777156E+03

File Structure:

  PERIODICITY : 0
  SYMMETRY    : 0


  MATERIAL MFUEL   
  MEDIUM   MFUEL    VOLUME  8.308106E+05


  *******************************************************
  * ISOTOPE  *   MASS (KG)   *   CIP    *    EQFMASS    *
  *******************************************************
  * U238     *  1.779265E+03 *   28.125 *  0.000000E+00 *

Thanks in advance.

2
  • 1
    Welcome to SO, please do add your efforts which you have put in order to solve your own problem; also do add expected output too in your post and let us know then. Commented May 30, 2019 at 6:19
  • Thanks for the reply, but the whole file has a more complex structure with respect to the attached. Commented May 30, 2019 at 6:49

2 Answers 2

2

You can use:

awk '$2=="U238" {print $4}' file > U238_out

or

awk '$2~/^U238$/ {print $4} file > U238_out

If U238 in 2nd field, print 4th field:

1.779265E+03
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, this code worked: awk '$2~/^U238$/ {print $4}' file >output
@yro both should work. If you like it, accept it. :)
0

you mean something like this?

sed '/\s*\*/!d;s/\s*[*][^*]*[*]\s*\([-+.E0-9]*\).*/\1/;/^$/d' file.txt

explanation

/\s*\*/!d          # delete line not started with [blank]*
;                  # separator for next sed command
s/                 # substitute
\s*                # ignore leading blanks
[*]                # search first *
[^*]*[*]           # ignore everything until the next *
\s*                # ignore blanks
\([-+.E0-9]*\)     # save number into arg1 (\1)
.*                 # ignore rest of line
/\1/               # print only arg1
;                  # separator for next sed command
/^$/d              # ignore empty lines (first 3)

output

3.099319E+02
1.274088E+01
1.779265E+03
3.789596E+02
1.760032E+02
5.049642E+01
5.002164E+01
4.777184E+00
2.594883E-19
2.594883E-19

1 Comment

Thanks everyone for your help!

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.