Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting top ten and bottom ten lines of a text file using a script 1

Status
Not open for further replies.

tempsup

MIS
Sep 10, 2003
26
0
0
GB
Hi to all,

I got a problem with scripts. I got a text file generated by some process and i need to remove the top ten and bottom ten lines. This has to be done using a script so that it can be performed automatically.. I really have no idea what to do. Will some kind hearted souls please help me????

Thanxxx.
 
I think this should help - it uses the ed editor

Code:
#!/bin/sh

# prepare a test file
awk 'BEGIN { for ( i = 1 ; i <= 30 ; i++ ) print i }' > tmp

# use ed to remove the last 10 and first 10 lines
ed -s tmp << HERE
^10
.,\$d
1,10d
wq
HERE

--
 
Or just with head and tail ( untested ) :

((count=`cat yourfile|wc -l` -10))
head -&quot;$count&quot; yourfile > tmp1
tail +10 tmp1 > yourfile


HTH


Dickie Bird (:)-)))
 
Or with awk...

awk '{a[++x]=$0}END{for(i=10;i<x-10;i++)print a[i+1]>FILENAME}' file1
 
Neat solution Ygor - so does ++x force a write to the array, and not an overwrite in the case of duplicates ?

Dickie Bird (:)-)))
 
Ooops, I just realised that I don't need x at all, just

awk '{a[NR]=$0}END{for(i=10;i<NR-10;i++)print a[i+1]>FILENAME}' file1
 
Hey guys, thanxxx for all your help. Couldn't hav done it without your!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top