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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-18-2007, 02:39 PM
|
#1
|
|
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:
|
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
|
|
|
|
05-18-2007, 03:25 PM
|
#2
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
Code:
sed -n '1p;$p' filename > newfile
|
|
|
|
05-20-2007, 12:42 PM
|
#3
|
|
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:
|
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.
|
|
|
|
05-20-2007, 01:59 PM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
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.
|
|
|
|
05-20-2007, 03:05 PM
|
#5
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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
|
|
|
|
05-20-2007, 06:26 PM
|
#6
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:34 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|