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
# 15  
Old 10-04-2009
Quote:
Surprisingly, when i tried your first code on SUN, i.e.
Code:
awk '{print > "file" (NR-1)%3+1}' infile
it worked pretty well!!
I'm always surprised when my code works pretty well!!

You don't have 2000 files, you have 3 files. Do you mean you want 3 * 10 files?

If so, then a for-loop, as you suggest would do fine.

(untested because my VM just went on the blink, something like...)
Code:
{ for( I = 1; I <= NF; I++ )
    print > "file" (NR-1)%3+1 "_" I
}


Last edited by Scott; 10-04-2009 at 01:55 PM.. Reason: typo
# 16  
Old 10-04-2009
well, (NR-1)%3+1 was just a test, the increments between lines were actually 3325 in my actual input file, so i replace 3 with 3325! so i am dealing with sth like 33250 files in total, sonsidering 10 fields!
cheers
# 17  
Old 10-04-2009
Yes, you're right, sorry, forgot you changed that.
# 18  
Old 10-04-2009
Hi Scottn,



I tried to change the code the get the desired outputs but it didnt happen. Assuming (NR-1%3+1), I'll bring a simple example to simplify what I am willing to have:



Code:
input file



1      7       13

2      8       14

3      9       15

4     10      16

5     11       17

6     12       18



output files



file1     file2     file3

1          2         3

4          5         6



file4       file5    file6

7            8        9

11          12      13



file7       file8     file9

13          14        15

16          17        18



So, for each fileld there will be three files containing only contents of field 1 and the same for other two fields. In general, the number of sequential numbers would be 3*3=9.



Do you have any tricks for this?
# 19  
Old 10-04-2009
bash code:
  1. awk '
  2. { for( I = 1; I <= NF; I++ ) {
  3.    print $I > "file" (NR-1)%FILES+1 + (I-1)*FILES
  4.  }
  5. }
  6. ' FILES=3 infile
# 20  
Old 10-05-2009
Your command line should look like this if you don't put the code in a file:

Code:
awk -v RF_CNT=2000 -v LN=2000 ' 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]);}' num.txt

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