If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how to separate odd even line# in a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-25-04, 15:17
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
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
Reply With Quote
  #2 (permalink)  
Old 08-25-04, 15:33
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sure it does:

Code:
FNR%2 { print >> fileO ; next} {print >> fileE }'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 08-25-04, 16:08
skd skd is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 08-25-04, 16:15
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 08-25-04, 16:22
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
thank you. it worked.

not sure why variable needed here for filename. is this possible using sed ?
Reply With Quote
  #6 (permalink)  
Old 08-25-04, 17:10
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #7 (permalink)  
Old 08-25-04, 17:19
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #8 (permalink)  
Old 08-26-04, 08:22
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Thanks man, I liked your new awk solution.

Thanks for all your time and help.
Reply With Quote
  #9 (permalink)  
Old 08-26-04, 09:48
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
  #10 (permalink)  
Old 08-26-04, 10:49
skd skd is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On