LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 05-18-2007, 02:39 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Shell scripting: print first line and last line only


Is there a way to print the first line of a file and the last line of a file with one command?

For example:

1
2
3
4
5

and I only want it to be

1
5

Assume that I don't know how many lines are in between the first and last line. The only way I could think of was to do:

head -n 1 filename > tempfile
tail -n 1 filename >> tempfile
 
Old 05-18-2007, 03:25 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
 sed -n '1p;$p' filename > newfile
 
Old 05-20-2007, 12:42 PM   #3
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
That works great! now, how about something a little more trickier. What if the list looked like this and I wanted to print from "/program/" to the end

apple
oranges
789
/program/
1
2
3
4
5

Assume that my list is random, but I always want to start from "/program/" until the end.

And also, would there be a way to start and stop at a certain point? For example, start at "/program/" and stop at "3"

Last edited by Micro420; 05-20-2007 at 12:44 PM.
 
Old 05-20-2007, 01:59 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You can use regular expression to express line numbers in sed. I.e:

'Normal':

sed -n '15,$p' infile
Would print line 15 (included) to end of file.

Using regexp:

sed -n'/\/program\//,$p' infile
Would print from the first found /program/ to end of file.

Hope this helps.
 
Old 05-20-2007, 03:05 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
In the case of searching from one regexp to another, you have a situation where the second regexp is a number.

Code:
If you use this...
sed -n '/\/program\//,3p' file.txt
sed is looking for a line number (3) which it already passed so,
you only get an output of
/program/
You get the desired result by putting both regexp inside / like this
Code:
sed -n '/\/program\//,/3/p' file.txt
 
Old 05-20-2007, 06:26 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Is there a way to print the first line of a file and the last line of a file with one command?
Code:
awk 'NR==1 {print}END{print }' file
Quote:
That works great! now, how about something a little more trickier. What if the list looked like this and I wanted to print from "/program/" to the end

apple
oranges
789
/program/
1
2
3
4
5

Assume that my list is random, but I always want to start from "/program/" until the end.
Code:
awk '/program/,$NR{print}' "file"
Quote:
And also, would there be a way to start and stop at a certain point? For example, start at "/program/" and stop at "3"
Code:
awk '/program/,/^3/{print}' "file"

Last edited by ghostdog74; 05-20-2007 at 06:30 PM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: How to add characters at the end of the line Micro420 Programming 7 05-18-2007 01:56 AM
Shell scripting :: comment N line in vi editor mahabooba Programming 4 07-28-2006 05:58 AM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 11:36 AM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 12:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration