The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete blocks of lines from text file nrbhole Shell Programming and Scripting 4 12-19-2007 06:17 AM
Delete a block of text delimited by blank lines when pattern is found gleu Shell Programming and Scripting 17 12-16-2007 11:30 PM
Delete lines containing text with sed umen Shell Programming and Scripting 2 08-01-2006 04:24 AM
Delete specific lines in a text file dniz High Level Programming 9 08-08-2005 08:30 AM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 07:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-09-2008
ragavendran31 ragavendran31 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
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 (permalink)  
Old 01-09-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
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 (permalink)  
Old 01-09-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
should that not be

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

to discard top 5 and bottom 5
  #4 (permalink)  
Old 01-09-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Quote:
Originally Posted by Tytalus View Post
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 (permalink)  
Old 01-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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 (permalink)  
Old 01-20-2008
ragavendran31 ragavendran31 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
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

Cheers,
Ragavendran
  #7 (permalink)  
Old 01-20-2008
Rajesh Kumar Rajesh Kumar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
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 .
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0