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
# 8  
Old 01-21-2008
The following also will help

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
#

Last edited by vkesineni; 01-21-2008 at 05:13 AM..
# 9  
Old 01-21-2008
Code:
 $ 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

# 10  
Old 01-21-2008
Quote:
without opening the file
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 ?
# 11  
Old 02-21-2008
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
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