How to delete first 5 lines and last five lines in all text files

Hi

I want to delete first five and last five lines in text files without opening the file and also i want to keep the same file name for all the files.

Thanks in advance!!!

Ragav

# count=`wc -l file|cut -f1 -d" "`
# more +6 file | head -$(( count-5 ))

should that not be

tail +6 file| head -$(( count-10 ))

to discard top 5 and bottom 5

more +num and tail +num does the same thing. yes you are correct. It should be -10 instead of -5.

Hi,

This one is used to delete certain number of line from the head and end of file in a given direcotory.

Hope it is useful for you.

Hi,

I tried with ur script.Its giving error msg as follows:

xpr:error opening "1, " for input
no such file or directory.

I have tried with following script (assume i have A.txt,B.txt in dir path):

echo "ur dir path:"
read dirp
cd $dirp
for i in *
do
awk 'FNR>5' $i > $i.new
done

With this i am able to delete first five lines.same way i want to delete last five lines also.

Any suggestions ....

Thanks in advance.

Note : one more doubt why am i getting A.txt~.new,B.txt~.new even my *.sh,*.txt files are not opened? I have seen the same scenario in MS word when you open one Word document until you close that doc will get one temp XXX.doc~.tmp something.

Ignore my silly doubts :wink:

Cheers,
Ragavendran

team$ cat numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
team$

team$ cat lines.sh
#!/usr/bin/ksh
echo "Enter No.of lines to remove from TOP : "
read top
echo "Enter No.of lines to remove from BOTTOM :"
read bottom

total=`wc -l < numbers`
endval=`expr $total - $bottom`
startval=`expr $top + 1`

echo "**** Showing lines $startval -- $endval **** \n"
sed -n ${startval},${endval}p $1
team$

team$ lines.sh numbers
Enter No.of lines to remove from TOP :
4
Enter No.of lines to remove from BOTTOM :
4
**** Showing lines 5 -- 11 ****

5
6
7
8
9
10
11
team$

put it in for loop to process all the files . :slight_smile:

This is just my idea.
------------cut here------------
#!/bin/ksh

echo "Enter No.of lines to remove from TOP : "
read top
echo "Enter No.of lines to remove from BOTTOM :"
read bottom
echo "Enter the file name:"
read fn

tot=`wc -l < $fn`
rmtop=`expr $tot - $top`
rmtail=`expr $tot - $bottom`

echo "Total lines"
echo "-----------"
cat $fn
echo "=================================================================================="
echo "Removing of last $bottom lines"
echo "------------------------------"
cat $fn|head -$rmtop
echo "=================================================================================="
echo "Removing of first $top lines"
echo "----------------------------"
cat $fn|tail -$rmtail
------------------Cut here-------------------------------
----------------------------------------------------------------
Out Put:-
---------
# sh test2.sh
Enter No.of lines to remove from TOP :
4
Enter No.of lines to remove from BOTTOM :
4
Enter the file name:
/venkat/scripts/file3
Total lines
-----------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Removing of last 4 lines
------------------------------
1
2
3
4
5
6
7
8
9
10
11

Removing of first 4 lines
----------------------------
5
6
7
8
9
10
11
12
13
14
15
#

 $ S=`awk 'END {print NR-4}' file.tmp`; sed -e '1,5 d' -e "$S,$ d" file.tmp > file.tmp.tmp ; mv file.tmp.tmp file.tmp

This would never be possible to read the contents without opening the file.

Be it any file reading utility like sed, awk, vi internally fopen is executed.

Did you mean not opening the file in an editor ?

hi,

to delete first 5 lines use:
tail +6 input.dat > output.dat

remember that +(x+1) if you want to delete first x lines.

baloo