Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2013
    Posts
    6

    Unanswered: awk - data manipulation

    Hi All,

    I'm having some trouble with data manipulation, and was hoping someone could chime in. I've got a single data file (see below), and am trying to grab the random name, and print it along side the data it correlates to.

    File with data:

    "random-name-ZONE80"

    "Time","Data1","Data2"
    "23-Nov-13 00:00:01 AM",1234,0.00000000
    "24-Nov-13 00:00:01 AM",1234,0.00000000
    "25-Nov-13 00:00:01 AM",1234,0.00000000
    "26-Nov-13 00:00:01 AM",1234,0.00000000

    "random-name-ZONE20"

    "Time","Data1","Data2"
    "25-Nov-13 00:00:01 AM",1234,0.00000000
    "26-Nov-13 00:00:01 AM",1234,0.00000000

    "random-name-ZONE30"

    "Time","Data1","Data2"
    "22-Nov-13 00:00:01 AM",1234,0.00000000
    "23-Nov-13 00:00:01 AM",1234,0.00000000
    "24-Nov-13 00:00:01 AM",1234,0.00000000
    "25-Nov-13 00:00:01 AM",1234,0.00000000
    "26-Nov-13 00:00:01 AM",1234,0.00000000
    "27-Nov-13 00:00:01 AM",1234,0.00000000
    "28-Nov-13 00:00:01 AM",1234,0.00000000
    "29-Nov-13 00:00:01 AM",1234,0.00000000
    Format wanted:

    "random-name-ZONE80","23-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name1-ZONE80","24-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name1-ZONE80","25-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name1-ZONE80","26-Nov-13 00:00:01 AM",1234,0.00000000

    "random-name-ZONE20","25-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE20","26-Nov-13 00:00:01 AM",1234,0.00000000

    "random-name-ZONE30","22-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","23-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","24-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","25-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","26-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","27-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","28-Nov-13 00:00:01 AM",1234,0.00000000
    "random-name-ZONE30","29-Nov-13 00:00:01 AM",1234,0.00000000
    Thanks in advance!

    -Kas

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Not awk but...
    Code:
    while read line                  
    do                               
    if [ "a$line" = "a" ]            
    then                             
            echo $line               
    else                             
    sub=`echo $line |cut -c2-5`      
    if [ "$sub" != "Time" ]          
    then                             
            if [ "$sub" = "rand" ]   
            then                     
                    rand=$line       
            else                     
                    echo $rand,$line 
            fi                       
            fi                       
    fi                               
    done
    I didn't know if you wanted to keep the blank lines.

  3. #3
    Join Date
    Nov 2013
    Posts
    6
    Quote Originally Posted by kitaman View Post
    Not awk but...
    Code:
    while read line                  
    do                               
    if [ "a$line" = "a" ]            
    then                             
            echo $line               
    else
    #grabs line 2 spaces above Time                             
    sub=`echo $line |cut -c2-5`  
    if [ "$sub" != "Time" ]          
    then                             
            if [ "$sub" = "rand" ]   
            then                     
                    rand=$line       
            else                     
                    echo $rand,$line 
            fi                       
            fi                       
    fi                               
    done
    I didn't know if you wanted to keep the blank lines.
    Thanks!! That worked like a charm. No worries on the blank lines.

    I mentioned awk, mainly because of performance. Then again, I don't know how awk will perform, as the file is over 3 million lines.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I've yet to see any scripting language out perform AWK for this kind of task, but if the answer that you've got works I wouldn't bother pursuing a solution in another language. I'm a pretty firm believer in using the simplest possible solution for any given problem, and a shell script is as simple as I know how to get in the *nix environment.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

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