Page 1 of 1

Copy every 'n' line from a txt files

Posted: 19 Aug 2010 08:16
by petenetman
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?

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

Posted: 19 Aug 2010 08:21
by ghostmachine4
download gawk for windows, then do this

Code: Select all

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

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

Posted: 19 Aug 2010 08:40
by petenetman
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?

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

Posted: 19 Aug 2010 09:18
by ghostmachine4
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.

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

Posted: 19 Aug 2010 09:23
by petenetman
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

??

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

Posted: 19 Aug 2010 10:04
by ghostmachine4
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.

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

Posted: 19 Aug 2010 10:30
by petenetman
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

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

Posted: 19 Aug 2010 11:10
by petenetman
:roll:

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

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

Yippeeee!!!!!!!!!

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

Posted: 19 Aug 2010 16:13
by orange_batch
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
)

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

Posted: 19 Aug 2010 18:37
by ghostmachine4
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