Deleting last records of a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting last records of a file
# 1  
Old 08-04-2009
Deleting last records of a file

can you please give shell script for daleting the last 7 records of file...
# 2  
Old 08-04-2009
no this is not a free script writing service. If you like help in learning how to write your own, that would be appropriate.
# 3  
Old 08-04-2009
Post what you have tried so far, and point out where you are having trouble.

I am sure that you will get some good replies.

People are much more likely to assist though if you can at least show some attempt.
# 4  
Old 08-04-2009
i have tried with the following script....

sed -n '/^$/q;p' inputfile

for writing a new file till it finds the empty record.... i am trying to write a code for deleting the last 7 records (which is a trailer records and a empty records) of a file and the file name itself contains the space....

Eaxample : File Name

Export Table.csv

Can you please help in this regard....

Last edited by vsairam; 08-04-2009 at 02:42 PM..
# 5  
Old 08-04-2009
check out 'man head' and 'man wc'
# 6  
Old 08-04-2009
You probably already figured out how to use what the above poster said, but here's some specifics:

$ wc -l inputfile
30 <---example record count
$ head -23 inputfile > outputfile

That should do it.
# 7  
Old 08-04-2009
I'll use the output of ls -al to demonstrate my quick and dirty method:

ls -al | tac | tail +8 | tac

The tac command is cat, backwards... it takes input and spits it out backwards by line. The command tail +x, where x is a positive integer, outputs the command's input starting at line x. Note that saying "chop off the first 7 lines" is the same as saying, "start showing output at line 8". Then, just tac it again and you're done. Like I said, this is quick and dirty... flip it upside down, chop its feet off, flip it rightside up again. Not recommended for any situation where even moderately good performance is needed! Oh, and so far as I've seen, if a system doesn't have tac, you can usually use tail -r in its place.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

2. Shell Programming and Scripting

Deleting the records based on the condition

Hi, Can any one help me, in deleting the records from the database table based on the following condition: script should take a configurable parameter as input. The input is nothing but “no. of years”. For example, if I enter 2 as input parameter, then the 2 year old records should get... (2 Replies)
Discussion started by: zxcjggu708
2 Replies

3. UNIX for Dummies Questions & Answers

Deleting records from .dat file

Hi, I need to delete one row from a .dat file. error processing column PCT_USED in row 1053295 for datafile /exp/stats/ts_stats.dat ORA-01722: invalid number This is used to load records using sql loader. Please let me know the procedure to do it. Regards, VN (3 Replies)
Discussion started by: narayanv
3 Replies

4. Shell Programming and Scripting

Perl : Deleting the records in the excel sheet

I have a excel sheet with contains the records as below.. also uploaded the input excelsheet and the output excel sheet(expected output). 44.0 M Hrly Avg (IN /... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. Shell Programming and Scripting

Deleting Duplicate Records

Hello, I'm have a file of xy data with over 1000 records. I want to delete both x and y values for any record that has the same x value as any previous record thus removing the duplicates from my file. Can anyone help? Thanks, Dan (3 Replies)
Discussion started by: DFr0st
3 Replies

8. Shell Programming and Scripting

deleting multiple records from a huge file at one time

I have a very big file of 5gb size and there are about 50 million records in there. I have to delete the records based on recrord number that I know fromoutside with out opening the file. The record numbers are very random like 5000678, 7890005 etc. Can somebody let me know how i can... (5 Replies)
Discussion started by: dsravan
5 Replies

9. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

10. UNIX for Dummies Questions & Answers

deleting records with a missing field

I had to delete rows when a record was missing a field. My solution was cut -c 202-402 ${dataFile} | awk '{if (substr($0,103,30) !~ /^ *$/) print $0} >> ${workFile} The cut is because they came two records to a row. Anyone want to offer a more elegant solution? (2 Replies)
Discussion started by: gillbates
2 Replies
Login or Register to Ask a Question