Unix/Linux Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Delete some lines in a file

UNIX for Advanced & Expert Users


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-26-2010
ponmuthu ponmuthu is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 8 January 2013, 5:53 AM EST
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Java Delete some lines in a file

hi i need to delete 3 lines (last 3 lines ) in a file


for ex :

>cat file
1
2
3
4
5
abc
def
ghi


i hav to delete last 3 line in above file

please help me
Sponsored Links
    #2  
Old Unix and Linux 07-26-2010
kurumi kurumi is offline
Resident Ruby Wrangler
 
Join Date: Dec 2009
Last Activity: 6 February 2016, 7:08 AM EST
Posts: 602
Thanks: 2
Thanked 83 Times in 78 Posts

Code:
 head -3 file



---------- Post updated at 12:26 AM ---------- Previous update was at 12:13 AM ----------


Code:
#!/bin/bash

OLDIFS=$IFS
IFS=$'\n'
var=($(<file))
len=${#var[*]}
for((i=0;i<$((len-3));i++))
do
  echo "${var[$i]}"
done
IFS="$OLDIFS"


Last edited by kurumi; 07-26-2010 at 01:25 AM..
Sponsored Links
    #3  
Old Unix and Linux 07-26-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts

Code:
# sed -n "1,$(echo "`sed -n '$=' infile` - 3" |bc) p" infile
1
2
3
4
5


Last edited by ygemici; 07-28-2010 at 02:35 PM..
    #4  
Old Unix and Linux 07-26-2010
STOIE's Unix or Linux Image
STOIE STOIE is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 16 September 2016, 4:20 AM EDT
Location: Canberra, Australia
Posts: 45
Thanks: 1
Thanked 0 Times in 0 Posts
I love how many different ways there are to do the same task for something like this...

head -n -3 is the best option...


Code:
#!/usr/bin/perl

while (<>) {
$x = $x . $_;
}
$x =~ sm/\n.*\n.*\n.*$//g;
print $x;

Sponsored Links
    #5  
Old Unix and Linux 07-28-2010
ponmuthu ponmuthu is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 8 January 2013, 5:53 AM EST
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
can u explain this cmd

sed -n "1,$(echo "`sed -n '$=' al` - 3" |bc) p" infile

i can't understand whr we hav to give file name
Sponsored Links
    #6  
Old Unix and Linux 07-28-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts
Quote:
Originally Posted by ponmuthu View Post
can u explain this cmd

sed -n "1,$(echo "`sed -n '$=' infile` - 3" |bc) p" infile

i can't understand whr we hav to give file name
# echo "`sed -n '$=' infile` - 3" |bc --->
sed -n '$=' infile ---> only print last line number ---> equal 8
and minus 3 eqaul 5
and sed -n "1,5p" --> only print 1 between 5 number line
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How get only required lines & delete the rest of the lines in file reva UNIX for Dummies Questions & Answers 7 06-01-2010 02:58 AM
Using SED to delete some lines from file akash_mahakode Shell Programming and Scripting 1 09-30-2009 06:20 AM
How to delete lines in a file that have duplicates or derive the lines that aper once necroman08 Shell Programming and Scripting 3 07-17-2009 05:07 AM
delete n last lines of a file ncatdesigner Shell Programming and Scripting 7 05-17-2008 10:07 AM
how can I delete lines without actually going into the file TRUEST Shell Programming and Scripting 5 03-19-2002 04:12 AM



All times are GMT -4. The time now is 11:51 PM.