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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete first 5 lines and last five lines in all text files
# 1  
Old 01-09-2008
Lightbulb 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
# 2  
Old 01-09-2008
Code:
# count=`wc -l file|cut -f1 -d" "`
# more +6 file | head -$(( count-5 ))


Last edited by ghostdog74; 01-09-2008 at 11:13 AM..
# 3  
Old 01-09-2008
should that not be

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

to discard top 5 and bottom 5
# 4  
Old 01-09-2008
Quote:
Originally Posted by Tytalus
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.
# 5  
Old 01-11-2008
shell

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.

Quote:
echo "please input how many lines you want cut from the head and end"
read line
echo "input the direcoty"
read dir
cd $dir
for i in *
do
n=`cat $i | wc -l`
m=`expr $n - $line`
rel=`expr $m + 1`
sed -e "1d" -e "$rel,\$d" $i > $i.bak

done
# 6  
Old 01-20-2008
Lightbulb

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 Smilie

Cheers,
Ragavendran
# 7  
Old 01-20-2008
Ragavendran, see if this helps

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 . Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

2. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

How to delete lines from text file?

hi guys, I have very large txt files (200GB) and just want to to delete the first two lines (headers). So far I used sed -i '1,2d' infile.txtbut this command always takes extremely long as it writes all again. Is there a better way to do it (ie just to delete the lines without writing all... (2 Replies)
Discussion started by: TuAd
2 Replies

5. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

6. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

7. UNIX for Dummies Questions & Answers

Delete vertical lines in an text file

Hi everybody! I need to delete several vertical lines in a huge text file. It should work like the example below. Delete the vertical lines 2 and 8. 123456789 masldfjla afsajfwel sajfljsaf safsarfrl sajfeljwq 1345679 msldfja asajfwl sjfljsf sfsarfl sjfeljq Is there a... (11 Replies)
Discussion started by: relaxo
11 Replies

8. Shell Programming and Scripting

Delete blocks of lines from text file

Hello, Hello Firends, I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file. zone abc { blah blah blah } zone xyz { blah blah blah } zone pqr { blah blah blah } (4 Replies)
Discussion started by: nrbhole
4 Replies

9. Shell Programming and Scripting

Delete lines containing text with sed

hello all I have bunch of files containing lines of text that surrounding by <# .......#> tags I like to delete this lines from the text files whiteout open the files , can it be done with sed ? or other unix tool (perl mybe )? (2 Replies)
Discussion started by: umen
2 Replies

10. Programming

Delete specific lines in a text file

Hi, experts, I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this? (9 Replies)
Discussion started by: dniz
9 Replies
Login or Register to Ask a Question