Copy every 'n' line from a txt files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
petenetman
Posts: 5
Joined: 19 Aug 2010 08:08

Copy every 'n' line from a txt files

#1 Post by petenetman » 19 Aug 2010 08:16

What I am trying to do is copy evey 6th line from a text files into another text file.

eg.

1.some txt
2.some txt
3.
4.some more text
5.even more txt
6.This is the line I want to copyThen evey 6th from here
1.
2
3.
Any ideas?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Copy every 'n' line from a txt files

#2 Post by ghostmachine4 » 19 Aug 2010 08:21

download gawk for windows, then do this

Code: Select all

c:\> gawk.exe "NR%6==0" myfile.txt

petenetman
Posts: 5
Joined: 19 Aug 2010 08:08

Re: Copy every 'n' line from a txt files

#3 Post by petenetman » 19 Aug 2010 08:40

Thanks

That doesn't do what I want it to do. The string skips the first 6 lines and then copies everything after that.

All I want to do is copy every 6th line

Any idea?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Copy every 'n' line from a txt files

#4 Post by ghostmachine4 » 19 Aug 2010 09:18

you want to copy every sixth line right? did you really run it?

Code: Select all

c:\test> more file
     1  line
     2  line
     3  line
     4  line
     5  line
     6  line
     7  line
     8  line
     9  line
    10  line
    11  line
    12  line
    13  line
    14  line
c:\test>  gawk 'NR%6==0' file
     6  line
    12  line



there you go, every 6th line for you. Otherwise, you will have to explain clearly with example output that you want.

petenetman
Posts: 5
Joined: 19 Aug 2010 08:08

Re: Copy every 'n' line from a txt files

#5 Post by petenetman » 19 Aug 2010 09:23

That's exactly what I want...

6 line
12 line
18 line
24 line

etc

When I ran it, it missed the first 6 lines then copied everything from line 7 so to speak. I ran the command line just as you stated and output it to a sepearate txt file i.e

c:\gawk.exe "NR%6==0" ips.txt > c:\temp\1\ippps.txt

??

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Copy every 'n' line from a txt files

#6 Post by ghostmachine4 » 19 Aug 2010 10:04

what do you mean by "missed the first 6th line" and "copied everything from line 7" ??

As you can see from my output, it does get the 6th and 12th line. there is no 7th,8th, 9th,10th and 11th line.

Why don't you show your command one more time, but don't redirect to a file. Just show the results to standard output and paste it here.

petenetman
Posts: 5
Joined: 19 Aug 2010 08:08

Re: Copy every 'n' line from a txt files

#7 Post by petenetman » 19 Aug 2010 10:30

Ok Sorry I tried it on a seperate text file and all the text got jumbled up for some reason. What I need to is pull the IP address out from the illustration below. The line above the second IP address is a blank.


1. 67.195.112.224
2.
3.
4.
6.
7.74.125.16.66
8.
9.
10.
11.
12.74.125.16.66
13.
14.
15.
16.
17.67.195.112.224
18.
19.
20.
21.
22.34.76.34.123

petenetman
Posts: 5
Joined: 19 Aug 2010 08:08

Re: Copy every 'n' line from a txt files

#8 Post by petenetman » 19 Aug 2010 11:10

:roll:

Thanks..... I've cracked it.

I put 4 blank lines in the top and changed the value to 5.

Yippeeee!!!!!!!!!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Copy every 'n' line from a txt files

#9 Post by orange_batch » 19 Aug 2010 16:13

Uhm, your illustration was missing line #5.

This could be done with DOS easily as well.

Code: Select all

@echo off&setlocal enabledelayedexpansion

:: Count each line. Find the remainder of dividing counter by 6. If remainder is 0, echo:%%x.
for /f "delims=" %%x in (ips.txt) do (
set /a counter+=1
set /a mod=!counter!%%6
if !mod!==0 echo:%%x
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Copy every 'n' line from a txt files

#10 Post by ghostmachine4 » 19 Aug 2010 18:37

petenetman wrote:Ok Sorry I tried it on a seperate text file and all the text got jumbled up for some reason. What I need to is pull the IP address out from the illustration below. The line above the second IP address is a blank.


1. 67.195.112.224
2.
3.
4.
6.
7.74.125.16.66
8.
9.
10.
11.
12.74.125.16.66
13.
14.
15.
16.
17.67.195.112.224
18.
19.
20.
21.
22.34.76.34.123


unless you are sure that every IP address is on every 6th line, otherwise, searching for the IP address is better than searching with line numbers

Code: Select all

C:\test>gawk --re-interval "/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/" ip.txt
1. 67.195.112.224
7. 74.125.16.66
12. 74.125.16.66
17. 67.195.112.224
22. 34.76.34.123


Post Reply