1835200 Members
2270 Online
110077 Solutions
New Discussion

How Do I

 
SOLVED
Go to solution
Paul Middleton
Frequent Advisor

How Do I

Greetings all,
.
Sorry to trouble you with what's probably a simple problem.
.
I have a large text file with information on FC10, Nike and AutoRAID arrays. I'm trying to pull informatin on these arrays and put the info in another file for manipulation. Problems is, I can't remember how to pull the next 100 lines from the original file and move them to the second file.
.
I currently am using cat to read the original file, then grep to find the line with the header information, and there I'm lost.
.
Any help would be greatly appreciated.
.
Paul Middleton
Dilligad - Do I Look Like I Give A Damn
8 REPLIES 8
Dario_1
Trusted Contributor
Solution

Re: How Do I

Hi Paul:

If you want to copy the last 100 lines from a file to a new file try the following:

tail -100 oldfile > newfile

Regards,

Dario
James R. Ferguson
Acclaimed Contributor

Re: How Do I

Hi Paul:

Here's one way. Assume that you want to begin extracting 100 lines from file where the string named 'token' first appears:

# awk '/token/ {print;for (i==0;i<5;i++) {getline;print}}' inputfile > outputfile

Regards!

...JRF...
S.K. Chan
Honored Contributor

Re: How Do I

You can use sed to pull section of the file based on line numbers .. for example if you want to extract lines 15 to lines 50 (both inclusive) you can do ..
$ sed -n '15,50p' the-file
To append these to another file, simply redirect the output with >>.
James R. Ferguson
Acclaimed Contributor

Re: How Do I

Hi Paul:

/corrected for 100-lines/

Here's one way. Assume that you want to begin extracting 100 lines from file where the string named 'token' first appears:

# awk '/token/ {print;for (i==0;i<100;i++) {getline;print}}' inputfile > outputfile

Regards!

...JRF...
Paul Middleton
Frequent Advisor

Re: How Do I

Thanks for the quick reply, Dario.
.
Problem is that the infomation can be anywhere in the file. What I need is a way to find the matching line in the text file, copy it and/or the next hundred lines to another file, and resume with my searching.
.
Regards,
Paul Middleton
Dilligad - Do I Look Like I Give A Damn
Chris Vail
Honored Contributor

Re: How Do I

You can use vi for this quite easily, instead of cat. Its a little anti-natural to learn, but easy enough once you get used to it.


Chris
F. X. de Montgolfier
Valued Contributor

Re: How Do I

Hi,

if you want to split the file in chunks of 100 lines, then you only have to use split:

split -l 100

will create files named aa ab and so on.

If you want to have the last 100 lines:
tail -n 100

If you want to have the 100 lines ending at line X:
head -n X |tail -n 100

You can combine all these commamds with a "grep" command, of course:

grep "header" |split -l 100
works...

Cheers,

FiX
Paul Middleton
Frequent Advisor

Re: How Do I

Dario - You were close, I'll be using your suggestion in the future.
S.K.Chan - Good idea, it will be usefull in the future.
Chris Vail - VI, are you nuts? I want to finish this project this year ! Remember the saying, Unix is a four-letter word and vi is the abbreviation.
F.X. de Montfolfier - Good ideas, they will help me down th line.
.
James R. Ferguson - Exactly what I needed. I was part way there, but the spaces were driving me nuts. (short drive, I could probably walk.)
.
Thanks to all for your help. The forums are as good as bouncing questions off the other C.E.'s when I was with HP in Cleveland.
.
Regards,
Paul Middleton
Dilligad - Do I Look Like I Give A Damn