0

I would like to get the 1st data on 1st column (i.e:1.20E-05) and replace the whole second column by minus with this 1st data. Then, i would like to delete the last data line and print to a new file.

I have a data such as below:

body comments Vxx yy zz 0 pwl (

1.20E-05 1 1.21E-05 1.1 1.22E-05 1.2 1.23E-05 1.3 1.24E-05 0 +)

expected output:

body comments Vxx yy zz 0 pwl (

0.00E-05 1 0.01E-05 1.1 0.02E-05 1.2 0.03E-05 1.3 +)

  • What did you try so far? – Romeo Ninov May 26 '22 at 04:30
  • Please add an actual sample (anonymised/censored if necessary for confidentiality) of your data to your question, properly formatted as code (e.g. with the {} button in the editor), and details/code (also formatted correctly) of what you've already tried, what happened when you tried it, and how that differed from what you expected to happen. – cas May 26 '22 at 10:12
  • It is not clear what your target is. Did you mix up columns and rows? Please rephrase. The number formatting in the desired output may not be achieved easily... – RudiC May 26 '22 at 15:22
  • i tried below: awk 'FNR>=2{$2=($2-1.20E-05)}; 1' file.txt > file1.txt and it works. but i have to manually check and key in the 1.20E-05. is there anyway to check the file and pass this value? hopefully it is clear to you with my trial above.. – bihqui tiang May 28 '22 at 07:33
  • I fail to see how the first data line of your output, 0.00E-05 1 has anything to do with 1.20E-05 1, mathematically. and your columns/rows description makes no sense, either. – Marcus Müller May 31 '22 at 09:48

1 Answers1

0

The question is not too clear. From my understanding, how far would this get you:

awk '
NR==1   {print
         next}
NR==2   {Subtrahend = $1
        }
        {if (Prev) print Prev
         $1   = sprintf("%.2g", $1-Subtrahend)
         Prev = $0
        }
END     {print $NF
        }' file
body comments Vxx yy zz 0 pwl (
0 1
1e-07 1.1
2e-07 1.2
3e-07 1.3
+)

After printing the header line, it saves the desired subtrahend from line 2, then subtracts it from all the lines' first field. Using the Prev variable postpones printout by one line, so the last one can be skipped / deleted (except for the +)).

Formatting is another story. You'd need to put some effort / processing in there, if need be.

RudiC
  • 8,969