Deleting all lines except last 500


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting all lines except last 500
# 1  
Old 05-05-2013
Deleting all lines except last 500

Hi All,

I want to write a script which first check the line counts of a file if its more than 500 it deletes rest except the last 500..

I tried sed but it looks sed counts line numbers from the head & not from tail.. May be I need a wc -l frist then apply if statement & pass on the line count to the sed statement.. Smilie

Can you please suggest me a way to do..

I am using Solaris 10 box...
# 2  
Old 05-05-2013
Maybe some like this works
Code:
awk '(s-NR)<500' s=$(cat file | wc -l) file

This User Gave Thanks to Jotne For This Post:
# 3  
Old 05-05-2013
man tail
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-05-2013
Yes, why make it complex when there are commands to do this.
Maybe I am in love with awk Smilie
tail -n 500
# 5  
Old 05-05-2013
tail is not deleting.. various use of tail is just displaying..

Code:
awk '(s-NR)<500' s=$(cat file | wc -l) file

is returning

"awk: can't open 664"

664 is actually the line number..
# 6  
Old 05-05-2013
cat t
Code:
one
two
three
four
five
six
seven
eight
nine
ten

Code:
tail -n 3 t
eight
nine
ten

Code:
awk '(s-NR)<3' s=$(cat t | wc -l) t
eight
nine
ten

This User Gave Thanks to Jotne For This Post:
# 7  
Old 05-05-2013
My purpose is to delete the lines except the last few..

For viewing the certain last few I would have used smaller one:
Code:
tail -3 t

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with deleting lines and saving them

I have a directory question where I ask the user which entry he wants to delete... echo "Which entry?" read entry sed '/^'$entry'/d' file This code does in fact delete that particular entry... HOWEVER, when I go to inquire about that same entry, it still populates like it was never... (4 Replies)
Discussion started by: itech4814
4 Replies

2. Shell Programming and Scripting

deleting lines in ex

Hello, im using ex to manipulate some text. Im trying to delete all the lines except those on which a certain regex can be found. the important part of the script: ex temp << 'HERE' g/regex/p HERE this command prints the lines I want to end up with, but it doesnt delete the others.... (2 Replies)
Discussion started by: drareeg
2 Replies

3. Shell Programming and Scripting

Fill the values between -500 to 500 -awk

input -200 2.4 0 2.6 30 2.8 output -500 0 -499 0 -488 0 .......... .......... .... -200 2.4 .... ... 0 2.6 (6 Replies)
Discussion started by: quincyjones
6 Replies

4. Shell Programming and Scripting

Deleting particular lines.

hi all, i have got a scenario in which i need to delete all the lines that ends with file names. e.g. input can be cms/images/services_icons/callback.png cms/cms/images/services_icons/sync.php cms/cms/images/services_icons and output should be cms/cms/images/services_icons ... (13 Replies)
Discussion started by: kashifv
13 Replies

5. UNIX for Dummies Questions & Answers

deleting lines by number

Is it possible to delete lines by their number? Also, I'd like to delete the last 3 rows of a file too. So from the front and back. Thanks. (9 Replies)
Discussion started by: cary530
9 Replies

6. Shell Programming and Scripting

Deleting processed lines

I have a log file that I am processing. This contains messages from and to a server (requests and responses). The responses to requests may not be in order i.e. we can have a response to a request after several requests are sent, and in some error cases there may not be any response message. ... (2 Replies)
Discussion started by: BootComp
2 Replies

7. UNIX for Dummies Questions & Answers

how to tail last 500 lines and vi them?

I have a very large log file and it speed up scrolling. so I want to tail last 500 lies and see using vi editor. tail -n 500 large_file | small_file | vi {}; this won't work. I'm very novice on Unix. TIA. (2 Replies)
Discussion started by: kang
2 Replies

8. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

9. Shell Programming and Scripting

deleting lines

I am trying deleting lines from a text file using sed.. sed '/OBJECT="ABC/{N;N;N;d; }' will do if i have to delete lines starting with Object and next 3 lines but I was looking for a way to delet lines starting with OBJECT and all the lines till it reaches a blank lines ..or it reaches a... (8 Replies)
Discussion started by: ajnabi
8 Replies

10. Programming

deleting lines

I am spooling a file from oracle and trying to delete the last line of the spooled file which I am unable to do. Problem is that this file can have multiple records each time and I have no way of knowing how many because the amount can vary. I had an idea of using a while loop to read the... (1 Reply)
Discussion started by: supercbw
1 Replies
Login or Register to Ask a Question