BSD Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsBSD Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old September 20th, 2004, 08:52 PM
Hokus Hokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Hokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
head command?

Hi, I'm currently taking a unix class and I'm stuck in one of our assignments.

Problem is, I need to extract X number of lines from a flat text file in bash.

i.e. lines 1-10 or line 11-20

My professor said use the head command, but I can't seem to make it extract anything but the first X number of lines. I need to be able to specify the starting line aswell.

Appriciate any help.

Mads.

Reply With Quote
  #2  
Old September 21st, 2004, 11:08 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
head or it brother tail cannot do what you want, see manpages.
the chip way is the fantastic 'sed'
sed -n '4,19p' inputfilename >outoutfilename
prints line 4 till line 19
nota start (4) have to be <= end (19)
this also works with strings
sed -n '/start/,/end/p' in >out
sure perl can it also

Reply With Quote
  #3  
Old September 21st, 2004, 12:45 PM
Hokus Hokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Hokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the response.

Mads.

Reply With Quote
  #4  
Old September 21st, 2004, 12:49 PM
Ctb's Avatar
Ctb Ctb is offline
An Ominous Coward
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2002
Posts: 4,425 Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 10 h
Reputation Power: 0
Code:
perl -e 'while(<>) { print "$_" if ++$L > 3 and $L < 20; }' < input_file

Reply With Quote
  #5  
Old September 28th, 2004, 12:15 PM
Hokus Hokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Hokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Found the method using head and tail:

i.e.

head -30 inputfilename | tail -20 > outputfilename

will output lines 11-30 from input i believe

Your example using sed makes more sense though. Thanks again.

Mads.

Reply With Quote
  #6  
Old October 15th, 2004, 04:55 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
hokus:
why do you spend time to invent the horror:
head -30 inputfilename | tail -20 > outputfilename
in *nix (and in life) there are a lot of alternatives to do
the same job.
to kill a fly... use an atomic bomb.

Reply With Quote
  #7  
Old October 21st, 2004, 12:51 PM
Hokus Hokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 Hokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well, why learn liniar algebra and stay up sleepless nights writing structured essays? .. because my grade depends on it, that's why

I don't attempt to reason with the course curriculum, I just do my best to learn and sometimes ask the experts (like yourself) when nessesary.

Cheers,

Reply With Quote
  #8  
Old October 22nd, 2004, 04:38 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
to grate my left ear
i can use my left hand
but over my head, coming down, i also can use my rigth hand.
both work.
no time for that, but sure 1+0 could be
(x + sin(y) - cos(z) + sqrt(q)) / bessel(a,c)

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsBSD Help > head command?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway