copying from line N1 to line N2 of a file in a series of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying from line N1 to line N2 of a file in a series of files
# 1  
Old 06-18-2009
copying from line N1 to line N2 of a file in a series of files

Dear community,

I'm quite a newbie with scripting, I have this problem:

I have a file with many lines and I want to copy the lines from 1 to N to file.1, from N+1 to 2N to file.2, and so on up to the end of the file

I have tried with something like this (N=43 in this example):

awk '{for (i=1;i<=1000;i++) if ((NR>=$i*43 -42)&&(NR<=$i*43)) print $0}' big_file >> file.$

but it doesn't output anything, neither does the following:

for ((i=1;i<=1000;i++))
do
awk '{ if ((NR>=$i*43 -42)&&(NR<=$i*43)) print $0}' big_file >> file.$
done

I've tried other ways, they all failed... do you have any suggestion?
# 2  
Old 06-18-2009
Try this...
Code:
i=1
N=100
inputfile=inputfile
total_lines=`wc -l $inputfile|awk '{print $1}`
flag=0
while [[ $flag -eq 0 ]]
do
   (( no_of_lines = $i * $N ))
   if [[ $no_of_lines -gt $total_lines ]]; then
      no_of_lines=$total_lines
      flag=1
   fi
   head -n $no_of_lines $inputfile | tail -n $N > file."$i"
   (( i = $i + 1))
done

# 3  
Old 06-18-2009
that worked, thanks a lot! Smilie
bests
p
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Copying characters on each line in a file

Hello, I would like to copy the first and third char on each line of a file and place them in the 14h and 17th char positions. The file name is listed first and is 6 char's and the dir name is second and also same char size on each line. The file has thousands of lines. Initial... (6 Replies)
Discussion started by: dmm
6 Replies

3. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

copying a line from a file using sed

Hi All, I need to copy a specific line from a file to another file. lets suppose the line number 13 of a file when I am writing the line number explicitly.. its working fine sed -n '13p' afile > anotherfile but, when inside a script, i am getting the line number value inside a variable... (4 Replies)
Discussion started by: gotamp
4 Replies

7. Shell Programming and Scripting

Copying a line from one file to other using vi editor

Hi Guys, the command ":yy" copies the line but it can be pasted in the same file. How can it be done if I want to copy it in other file. (2 Replies)
Discussion started by: ajincoep
2 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

10. Shell Programming and Scripting

Copying/Extracting from line A to line B

Can any one please help me to copy file content between particualr line numbers. (3 Replies)
Discussion started by: engineer
3 Replies
Login or Register to Ask a Question