Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2003
    Posts
    71

    Unanswered: how to separate odd even line# in a file

    i am newbie here and looks that awk doesnot support modulus function or i am having some syntax error

    > fileE
    > fileO
    #
    #awk '{ for ( i = 1,I<NR,i++ )
    # if (NR%2) == 0) print $0 >> fileE else print $0 >> fileO
    # }' testNC
    #
    awk -f sd.awk testNC

    // my purpose is to separate out even and odd lines of file into two files

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    sure it does:

    Code:
    FNR%2 { print >> fileO ; next} {print >> fileE }'
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Sep 2003
    Posts
    71
    that looks pretty simple. i am still making some mistake

    awk ' FNR%2 { print >> fileO ; next} {print >> fileE }' testNC
    //

    awk: A print or getline function must have a file name.
    The input line number is 1. The file is testNC.
    The source line number is 1.

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk -v fileO=myOdd -v fileE=myEven 'FNR%2 { print >> fileO ; next} {print >> fileE }' testNC
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  5. #5
    Join Date
    Sep 2003
    Posts
    71
    thank you. it worked.

    not sure why variable needed here for filename. is this possible using sed ?

  6. #6
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Quote Originally Posted by skd
    thank you. it worked.

    not sure why variable needed here for filename. is this possible using sed ?

    The variables are used so that the filenames are NOT hard-wired in the script itself, but rather supplied on the command line - this is simply a matter of a personal taste.

    The same can be re-written as:

    Code:
    nawk 'FNR%2 { print >> "fileO" ; next} {print >> "fileE" }' testNC
    I cannot think of a way to do in sed, but I've been wrong before.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  7. #7
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    or you can do it using ksh:

    Code:
    #!/bin/ksh
    
    sed = testNC | sed 'N;s/\n/:/' | while IFS=: read num line
    do
       (( num % 2 )) && echo "$line" >> "fileO" || echo "$line" >> "fileE"
    done
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  8. #8
    Join Date
    Sep 2003
    Posts
    71
    Thanks man, I liked your new awk solution.

    Thanks for all your time and help.

  9. #9
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Quote Originally Posted by vgersh99
    I cannot think of a way to do in sed, but I've been wrong before.
    You could use the holdspace to determine if it is odd or even.
    In the example below, I assume that if the holdspace is empty, it is an odd line. I would then substitute the empty string for a "." , swap back to the pattern space, output the value of pattern space to "oddFile" and then branch to the end of the script. For the next line, the holdspace now contains a ".", so the script executes ths second block which does the opposite (blanks out the holdspace and writes to "evenFile").

    Personally, I'd stick with awk!

    Code:
    sed -n 'x
    /^$/{
    s/^/./
    x
    w oddFile
    b theEnd
    }
    /./{
    s/.//
    x
    w evenFile
    }
    :theEnd' inputFile

  10. #10
    Join Date
    Sep 2003
    Posts
    71
    awk solutions seems short and straight to me.
    but i appreciate you showing and explaining your sed solution.
    Thanks Damian & vgersh99

Posting Permissions

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