awk in complex number data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk in complex number data
# 1  
Old 05-09-2017
awk in complex number data

Hi,

I'm trying to transform my data from the following format:
Code:
eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472+6.3505226053266e-6i

to this:

Code:
eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472, +6.3505226053266e-6

so I can plot it with GnuPlot.

how do I change "a+bi" to "a, b" using awk?

Any help is appreciated. Smilie

---------- Post updated at 06:22 PM ---------- Previous update was at 05:42 PM ----------

I could solve it myself using these two sed commands:
sed "s/+/, &/;s/i/ /" eps.out > eps2.out

Last edited by RudiC; 05-09-2017 at 06:36 PM..
# 2  
Old 05-09-2017
What if the imaginary part is negative as in the cube root of -1?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-09-2017
Quote:
Originally Posted by RudiC
What if the imaginary part is negative as in the cube root of -1?
In that case I wait for your response... Smilie
For my first file I didn't have negative imaginary part.
# 4  
Old 05-10-2017
Try
Code:
sed -r 's/([^ e])([+-])/\1, \2/g; s/i//g' file
eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472, +6.3505226053266e-6
eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472, -6.3505226053266e-6


Last edited by RudiC; 05-10-2017 at 02:58 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-10-2017
I'm sorry, but waiting for us to tell you what the format is for your input data seems extremely backwards. You should be able to either show us the format that is used to print the last field in your input file, show us the output for a data sample with a negative imaginary part, or explicitly state that there will NEVER be any negative imaginary parts.

In addition to what RudiC suggested and assuming that if there are any negative imaginary parts in your complex numbers, they use a - instead of a + to separate the real and imaginary parts; you could modify your sed command to cover both cases (assuming you are using a standards conforming sed with an extension that allows multiple substitute commands to be separated on one line by a semicolon [as was used in your code]):
Code:
sed 's/\([0-9]\)\([-+][0-9]\)/\1, \2/; s/i//' eps.out > eps2.out

or, if there could be more than one complex number on a line:
Code:
sed 's/\([0-9]\)\([-+][0-9]\)/\1, \2/g; s/i//g' eps.out > eps2.out

If you're using a GNU sed which does not always adhere to the standards, you might need to use:
Code:
sed --posix 's/\([0-9]\)\([-+][0-9]\)/\1, \2/g; s/i//g' eps.out > eps2.out

to make it work. If you would tell us what operating system and shell you're using when you submit questions like this, those of us trying to help you would be able to determine which code suggestion might work for you without making assumptions about what OS you might be using.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk processing of variable number of fields data file

Hy! I need to post-process some data files which have variable (and periodic) number of fields. For example, I need to square (data -> data*data) the folowing data file: -5.34281E-28 -3.69822E-29 8.19128E-29 9.55444E-29 8.16494E-29 6.23125E-29 4.42106E-29 2.94592E-29 1.84841E-29 ... (5 Replies)
Discussion started by: radudownload
5 Replies

2. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

3. Shell Programming and Scripting

Complex awk problem

hello, i have a complex awk problem... i have two tables, one with a value (0 to 1) and it's corresponding p-value, like this: 1. table: ______________________________ value p-value ... ... 0.254 0.003 0.245 0.005 0.233 0.006 ... ... ______________________________ and a... (6 Replies)
Discussion started by: dietmar13
6 Replies

4. Shell Programming and Scripting

Help with Complex Awk.

Hi, I have a file. In this file when ever the word "ABC" occurs at position from 25 and 34 I would like to replace the value at postion 100 to 5 for the first 1000 rows only. I have written the following Awk command. nawk 'substr($0,25,9)=="ABC" {print $0}' filename The above command... (4 Replies)
Discussion started by: pinnacle
4 Replies

5. Shell Programming and Scripting

extract complex data from html table rows

I have bash, awk, and sed available on my portable device. I need to extract 10 fields from each table row from a web page that looks like this: </tr> <tr> <td>28 Apr</td> <td><a... (6 Replies)
Discussion started by: rickgtx
6 Replies

6. Shell Programming and Scripting

awk script (complex)

picked this up from another thread. echo 1st_file.csv; nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } }' OFS=, 1st_file.csv *.csv | sort -r i need to use the above but with a slight modification.. 1.compare against 3 month... (25 Replies)
Discussion started by: slashbash
25 Replies

7. Shell Programming and Scripting

complex Awk Question

Hi, I have a file look likes this : --->start hir Trace file: pudwh_ora_9998.trc Sort options: fchela exeela ***************************************************************count = number of times OCI procedure was executed cpu = cpu time in seconds executing elapsed = elapsed... (3 Replies)
Discussion started by: yoavbe
3 Replies

8. UNIX for Dummies Questions & Answers

Gathering data from complex/large dataspreads .txt format

Hi, I'm working on gathering information stored in .txt files. The format of the data within the .txt files is shown in the picture uploaded with this post. Sections like the one pictured are repeated (with different data, same format) many times within each .txt file but each section is of data... (4 Replies)
Discussion started by: p43hd
4 Replies

9. Shell Programming and Scripting

Sort complex data

Hi, Can someone here help sorting the following data in numeric order? INPUT: FIRST abc(3) def(13) fgh(1) ijk(6) abc(2) SECOND dfe(10) abc(4) hij(19) tlm(1) hij(1) hub(10) abc(1) fed(3) OTHERS hij(10) mok(4) bub(19) hij(1) abc(2) abc(15) abc(1) hij(3) OUTPUT: FIRST def(13) ijk(6)... (12 Replies)
Discussion started by: need_help
12 Replies

10. Shell Programming and Scripting

Complex use with awk

Hi , I have file named docs.txt The content of the file look like this: DOC disk location Size ======= ===== ============= ========= TXT A /dev/dm-1 10 TXT B /dev/dm-2 10 BIN C ... (3 Replies)
Discussion started by: yoavbe
3 Replies
Login or Register to Ask a Question