I have a question regarding the awk/sed operators. I have a big file which has following set of lines repeated

Expression loweWallrhoPhi :  sum=-6.97168e-09
Expression leftWallrhoPhi :  sum=6.97168e-09
Expression lowerWallPhi :  sum=-5.12623e-12
Expression leftWallPhi :  sum=5.12623e-12
Expression loweWallrhoUSf :  sum=-6.936e-09
Expression leftWallrhoUSf :  sum=6.97169e-09
Expression lowerWallUSf :  sum=-5.1e-12
Expression leftWallUSf :  sum=5.12624e-12

I want to extract value after sum in each case in separate file. Is it possible to do so at one go?


With grep command:

grep -oP 'sum=\K.*' inpufile > outputfile

grep with -P(perl-regexp) parameter supports \K, which use to ignoring the previously matched characters.

With awk command:

awk -F"=" '{print $NF}' inputfile > outputfile

Awk NF gives you the total number of fields in a record/line. So the last value of that is the last field number in a record/line.

With sed command:

sed 's/^.*sum=//' inpufile > outputfile

^.*=sum replace all characters(.*) between starting of line(^) and last characters(sum=) with whitespace char.



If you want to save each value into a separate file, use above commands into a while loop:

while read line; do
    echo "$line" | grep -oP 'sum=\K.*'     > $(echo "$line" |awk '{print $2}');
   #echo "$line" | awk -F"=" '{print $NF}' > $(echo "$line" |awk '{print $2}');
   #echo "#line" | sed 's/^.*sum=//'       > $(echo "$line" |awk '{print $2}');
done < file
| improve this answer | |
  • That includes the sum= and that is not the same as the value after sum= – Anthon Nov 11 '14 at 10:39
  • OP wants the value after sum, also that awk description of NF is awful. – user78605 Nov 11 '14 at 10:41
  • 1
    To complete this very good answer, you can also use cut: cut -d'=' -f2 file. – fedorqui Nov 11 '14 at 13:24
  • This is a very good answer. I liked it. Thank you. – Jaffer Wilson Jan 31 '17 at 7:45

If I correctly understand the question you want to get only values after =, and store the these values in separate files, based on second field(?). If I'm right try something like this:

$ awk -F'[ =]' '{print $6>"file_"$2".txt"}' file

The result:

$ ls -1

$ cat  file_leftWallPhi.txt
| improve this answer | |
  • @KasiyA I cannot reproduce your problem with GNU awk 4.0.2. The command from my answer works also with -c option (compatibility mode with traditional UNIX awk where GNU extensions are disabled). Please be sure you have updated input file as the original question was edited and empty lines deleted. – jimmij Nov 11 '14 at 11:38

You can do it by sed

sed -E 's/^.* (\S+)\s*:.*=(\S+)/echo "\2" > "\1".txt/' file | bash

The script find out two pieces in line:

  1. between spaces and : and should contain some(more then 0) non-space symbols ;
  2. some(more then 0) non-space symbols after =;

and format from its in execution command which transfered through the pipe to bash

| improve this answer | |
  • A much more versatile answer. – duanev Aug 2 '16 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.