Copy every 'n' line from a txt files
Moderator: DosItHelp
-
- Posts: 5
- Joined: 19 Aug 2010 08:08
Copy every 'n' line from a txt files
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?
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?
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Posts: 5
- Joined: 19 Aug 2010 08:08
Re: Copy every 'n' line from a txt files
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?
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?
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Copy every 'n' line from a txt files
you want to copy every sixth line right? did you really run it?
there you go, every 6th line for you. Otherwise, you will have to explain clearly with example output that you want.
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.
-
- Posts: 5
- Joined: 19 Aug 2010 08:08
Re: Copy every 'n' line from a txt files
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
??
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
??
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Copy every 'n' line from a txt files
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.
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.
-
- Posts: 5
- Joined: 19 Aug 2010 08:08
Re: Copy every 'n' line from a txt files
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
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
-
- Posts: 5
- Joined: 19 Aug 2010 08:08
Re: Copy every 'n' line from a txt files
Thanks..... I've cracked it.
I put 4 blank lines in the top and changed the value to 5.
Yippeeee!!!!!!!!!
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Copy every 'n' line from a txt files
Uhm, your illustration was missing line #5.
This could be done with DOS easily as well.
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
)
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Copy every 'n' line from a txt files
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