Every nth line with different starting point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Every nth line with different starting point
# 8  
Old 10-03-2009
Thanks!!
well, the actual text file has 59850 records where I have to pick every 3325 records and output to different files!! So, i have to place some number around 18 which still gives me the same error code " too many output files" !!
Can you please help me more? Also, what if i have more than one field in the input file? will a for loop inside AWk structure do?
# 9  
Old 10-03-2009
Actually, I've just run this with 600,000 records using the original awk

Code:

awk '{print > "file" (NR-1)%3+1}' infile

and it did not complain, which in hindsight makes sense since it's only opening three files.

And with 10000 files:
Code:
 awk '{print > "file" (NR-1)%10000+1}' infile

No problem there either.

You can have as many fields as you like, it writes the whole record to the files.

Which OS / awk version are you using?

Last edited by Scott; 10-03-2009 at 08:16 PM..
# 10  
Old 10-03-2009
Here a version that works without running into the open file limit. It's an awk script and should be in a file:

Code:
BEGIN {
        print RF_CNT;
        print LN;
}

(NR <= RF_CNT) {                # output first records
                a[NR] = "file" NR;
                print >a[NR];
                close(a[NR]);   # expensive but stays under fopen limit
}

((i = NR%LN +1) in a) {         # see if this line is a candidate
                print >>a[i];   # add it
                close(a[i]);    # close it to stay under fopen limit
}

Use this command line for it:

Code:
awk -f num.awk -v RF_CNT=20 -v LN=2000 num.txt

The value of RF_CNT is the number of files to make. The first line of each file will contain line N of the input file (ie. file 4 contains line 4 of the input file).

The value of LN is the line offset/modulo to determine if line need to go to a file and which file.

num.txt is input file. I used a file with the numbers 1-8000 on each line.

It opens and closes the output files to avoid the open file limits...

The BEGIN clause is just to confirm inputs and can be removed....
# 11  
Old 10-03-2009
Dear Scottn,

I switched to SUN systems and the code 'kinda' worked. I mean, the numer of output files were correct but I had only one line in each output file. For the test case, I had one input file with 40950 records and I wanted multipliations of 2275 to be printed in each file which should result in 40950/2275=18 lines in each file.
In the last file, file2275, i had the last line of input file, for the file2274 i had 40949th line of the input file printed and so on to file1!!!!
I do not know what's wrong and am really confused.
The code works for small number of modulos like up to 10 but doesn't like big numbers!!

JP2542a

I tried your code but it gives me an error on the line begining with (( i = NR....). syntax error and bailing out near this line!!

Thank you so much

Last edited by nxp; 10-03-2009 at 10:52 PM.. Reason: adding a note
# 12  
Old 10-03-2009
We'll get there!

Use /usr/xpg4/bin/awk on Solaris. I say this every time the word Solaris crops up. Gives me time to think of something more useful to say!!

I will try it on Solaris tomorrow.

Cheers
# 13  
Old 10-03-2009
I ran the script on both CentOS and cygwin and it worked. What exactly did you type and what was the exact output?
# 14  
Old 10-04-2009
Dear Scottn,

Surprisingly, when i tried your first code on SUN, i.e.
Code:
awk '{print > "file" (NR-1)%3+1}' infile

it worked pretty well!! whereas when i tried the code with close (out) the outputs were garbage!
So, if i have 10 fields and i want to have each field processed seperatly, what should i do? By this I mean if 2000 files is generated from one column I would like to end up with 2000*10 files. Does a for loop
(for i=1;i<=FN;i++) works? if so, where in the code?

Dear JP2542a,
The following is the code i've used. The bug shows up when i try it on Solaris machines while when i run it on SUNs it doesn't complain but it doesn't generate any files. Don't bother yourself if it's sth wierd coz i've been succefully run Sottn's code.

I should very much appreciate your helpSmilie
I'll probably get back here with some other questions.

Cheers


Code:
  BEGIN {print RF_CNT;print LN;}(NR <= RF_CNT) { a[NR] = "file" NR;print >a[NR];close(a[NR]);}(( i = NR %LN + 1 ) in a ) {print >> a[i];close(a[i]);}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

4. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

5. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. UNIX for Dummies Questions & Answers

how to delete nth line

can you please tell me (1) how to delete 1st and 3rd line only from a file. (2) How to delete last 4 lines in a file that has 2 blank lines out of last 4 lines. Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

8. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

9. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

10. Shell Programming and Scripting

Manage starting point in shell script.

Hi, I'd like to run a script with an optional starting point. Meaning that if no parameter for the script => Do everything, otherwise start from the point specified in the parameter and continue till the end. I thought of using the "case ..." but I have no result. Script: # ---------------... (6 Replies)
Discussion started by: ai_dba
6 Replies
Login or Register to Ask a Question