Find the answer to your Linux question:
Results 1 to 6 of 6
I have a file of complex numbers written in their real+imaginary parts but without spaces and of different length and with or without exponentials, such as: 0.4672547e-7+.95475538e-3i .4574785e-1+0.274636i 1.58365834-.5982347e-2i ...... ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    3

    How can I awk, grep, sed, etc. the real and Im parts of a complex number?!?

    I have a file of complex numbers written in their real+imaginary parts but without spaces and of different length and with or without exponentials, such as:

    0.4672547e-7+.95475538e-3i
    .4574785e-1+0.274636i
    1.58365834-.5982347e-2i
    ......

    I would like to separate the above into real an imaginary parts as follows:

    0.4672547e-7
    .95475538e-3
    .4574785e-1
    0.274636
    1.58365834
    -.5982347e-2

    I tried out built-in functions for string manipulation like awk, sed, grep, etc. but could not find a way to do that. Can anyone help?

    Thanks in advance.

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Thinking pretty quickly here, but I think awk can do it. If there are spaces between the real part and the plus/minus and the imaginary part, then awk should be able to pull out each number individually.

    Code:
    awk -F " " '{print $1 "\n" $3}' yourfile.txt
    PS:: I really hope I'm not helping you cheat with your homework here....
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    3
    hmmm... no this separates only the lines, but the real and imaginary parts remain "glued" together.

  4. #4
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    The line I gave you will only separate them correctly if there are spaces in the lines (as I said).

    The following will work fine.
    Code:
    x + yi
    Whereas the following won't,
    Code:
    x+yi
    Maybe you could use sed to add those spaces? Search for instances of "+" or "-" that don't have an "e" before them, and replace them with " + " or " - ". Then my original awk line will work.
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Hey There,

    This will work perfectly for your sample script, but is very specific. If it doesn't do the trick, throw out some more random data

    # sed -e 's/-\./\n-/' -e 's/\+/\n/' -e 's/i$//' YOURFILE
    0.4672547e-7
    .95475538e-3
    .4574785e-1
    0.274636
    1.58365834
    -5982347e-2


    Best wishes,

    Mike
    Last edited by eggi; 05-06-2008 at 08:06 PM. Reason: File Name not clear in my example

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    This recognizer seems fairly robust:
    Code:
    #!/usr/bin/perl
    
    # @(#) p2       Demonstrate pattern matching floating point.
    #
    # Adapted from 'The AWK Programming Language", page 40.
    
    use warnings;
    use strict;
    
    my($debug);
    $debug = 1;
    $debug = 0;
    
    my ($sign,$decimal,$fraction,$exponent,$number);
    $sign = "[+-]?";
    $decimal = "[0-9]+[.]?[0-9]*";
    $fraction = "[.][0-9]+";
    $exponent = "([eE] $sign [0-9]+)?";
    # $number = sign "(" decimal "|" fraction ")" exponent;
    $number = "$sign ( $decimal | $fraction ) $exponent";
    print " numpat = $number\n" if $debug;
    
    my($lines) = 0;
    
    while ( <> ) {
            chomp;
            $lines++;
            print "$1\n" while m{($number)}gxms;
    }
    
    print STDERR " ( Lines read: $lines )\n";
    
    exit(0);
    Producing (from your data on file "data1"):
    Code:
    % ./p2 data1
    0.4672547e-7
    +.95475538e-3
    .4574785e-1
    +0.274636
    1.58365834
    -.5982347e-2
     ( Lines read: 3 )
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...