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
# 1  
Old 10-03-2009
Hammer & Screwdriver Every nth line with different starting point

Hi every one,

I am trying to generate different files from a column of numbers with the following format, as an example:


main file(input)
Code:
1                                           
2                                         
3                                          
.                                            
.
2000
2001
2002
.
.
4000
4001
4002
.
.
6000
6001
6002


The outputs
Code:
file1     file2        file3 
1         2            3
2000      2001         2002
4000      4001         4002
6000      6001         6002

So, the idea is to pick every 2000th data (with different starting line) and put them into a new file, this process goes down to the end of file.
I've tried AWK/sed with while loops but it does not work because AWK/sed dont accept variables ($i) in their arguments. The code is in CSHELL.

I very much appreciate your help

Last edited by Franklin52; 10-03-2009 at 03:50 PM.. Reason: Please use code tags!!
# 2  
Old 10-03-2009
Hi.

Let me know if I misunderstood you:

Code:
awk '{A = A?A " "$1:$1} (NR%LN+1) == LN {print A > "file" ++C; A=""} END { if (A) print A > "file" ++C}' LN=2000 infile


Last edited by Scott; 10-03-2009 at 02:54 PM.. Reason: missed the END part
# 3  
Old 10-03-2009
Thank you so much Scottn for your prompt reply.
Is that supposed to creat 3 different files with the following formats:

The outputs

file1
Code:
1 
2000
4000 
6000 
 
file2 
2 
2001
4001
6001
 
file3
3
2002
4002
6002

If so, what's A? or what does A=A?A do?

Thanks again

Last edited by Franklin52; 10-03-2009 at 03:51 PM.. Reason: Please use code tags!!
# 4  
Old 10-03-2009
Hi.

Yep! I misunderstood you.

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

# 5  
Old 10-03-2009
This works perfectly for limited number of files, however if I am to deal with the entire data, actual case, there will be 2000 ouput files which the code gives me error, " too many output files", e.g. if I try 2000 instead of 3 in NR-1%3+1 it doesn't like it!!

Any idea for the entire data?

cheers
# 6  
Old 10-03-2009
Code:
awk '{close(out); out="file" (NR-1)%3+1; print > out}' infile

# 7  
Old 10-03-2009
Thanks vger. I knew I should have had more than 30 records in my test scenario!

Code:
awk '{out="file" (NR-1)%3+1; print > out; close(out) }' infile


Last edited by Scott; 10-03-2009 at 06:14 PM..
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