LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2008, 12:15 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
exclude last line


Is there a way that I could exclude last line from a file,without obtaining its length.

I don't have less command

like

Code:
cat $file | exclude last line > $outputfile
Thanks
 
Old 07-24-2008, 12:24 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
sed '$d' filename > newfilename

OR

sed -i '$d' filename

The latter is "edit in place".
 
Old 07-24-2008, 01:27 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Code:
cat $file | head -n -1
 
Old 07-24-2008, 02:10 PM   #4
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
when I run
Quote:
cat $file | head -n -1
I am getting the following error. Thats what I tried in the first place
Quote:
head: Invalid "-n file.tst" option
usage: head [-n #] [-#] [filename...]
Quote:
sed '$d' filename > newfilename
When I run this alone, it works fine. What if I have set of "sed commands", then it doesn't work? Is piping only the option..
like eg..

Code:
sed -e '/^x/d' -e '$d' $file > $newfile
 
Old 07-24-2008, 02:20 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by tostay2003 View Post
Is piping only the option..
like eg..

Code:
sed -e '/^x/d' -e '$d' $file > $newfile
That's not "piping". The syntax above is just a way to tell SED to do two different things. If you actually run one SED command and then pipe the output to another one, you may get a different answer.

In my experience, there's only one way to get this stuff to "stick" to you brain---run lots of tests on simple commands.
 
Old 07-24-2008, 04:41 PM   #6
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
That's not "piping". The syntax above is just a way to tell SED to do two different things. If you actually run one SED command and then pipe the output to another one, you may get a different answer.

In my experience, there's only one way to get this stuff to "stick" to you brain---run lots of tests on simple commands.
sorry didn't post my question properly?

Is piping the only option

Code:
sed -e Task1 -e Task2 -e Task3 $file | sed '$d' > $Newfile
Is there any way we could perform without piping???


Lastly

Where can I get the definitions for -

Quote:
$1 ---- first argument
$2 ---- second argument
$* ---- all argument
$# ---- number of arguments
$d ---- lastline
Hard to google on them

Last edited by tostay2003; 07-24-2008 at 04:45 PM.
 
Old 07-24-2008, 08:49 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
http://sed.sourceforge.net/grabbag/
also
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

BTW, in answer to the orig qn
Code:
cat test.txt

test file for print
asd
zxc
bnm

head --lines=-1 test.txt

test file for print
asd
zxc

Last edited by chrism01; 07-24-2008 at 08:54 PM.
 
Old 07-24-2008, 09:21 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by tostay2003 View Post
when I run
Code:
cat $file | head -n -1
I am getting the following error. Thats what I tried in the first place
cat is useless here.
Code:
head -n -1 file
if you have error, make sure you have GNU head.


Quote:
When I run this alone, it works fine. What if I have set of "sed commands", then it doesn't work? Is piping only the option..
like eg..

Code:
sed -e '/^x/d' -e '$d' $file > $newfile
For large files, sed with '$d' runs slower than head.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
tar --exclude --exclude-from cefn Linux - Software 4 10-11-2005 07:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration