The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script that extract some lines from a file lookin into another heartwork Shell Programming and Scripting 7 01-25-2008 06:57 AM
Extract lines from a file automatically. Please a Help alexcol Shell Programming and Scripting 8 12-16-2006 05:25 PM
How to extract a sequence of n lines from a file 0ktalmagik Shell Programming and Scripting 4 06-29-2006 11:24 PM
How to extract many lines from a file, typically the 1000 last kingkong UNIX for Dummies Questions & Answers 3 11-23-2005 04:20 AM
extract specific lines from file apalex UNIX for Dummies Questions & Answers 2 05-15-2001 09:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-08-2004
beilstwh beilstwh is offline
Registered User
  
 

Join Date: Jul 2004
Location: New York State
Posts: 65
how to extract a range of lines from a file

I am reading a file that contains over 5000 lines and I want to assign it to a shell variable array (which has a restriction of 1024 rows). I had an idea that if I could grab 1000 record hunks of the file, and pipe the records out, that I could perform a loop until I got to the end and process 1000 records at a time. For example

lines 1-1000
lines 1001-2000
lines 2001-3000
etc...

Does anyone know of a unix command that will allow the user to return a range of lines from a file to standard output?
The command that I was trying was.

set -A CustNo `cut -f1-19 -d',' -s RAGEFF.lst|sed 's/,/ /g'|sed 's/\L//g'|nawk '{ if (NF == 19) {print $1}}' `

Any help would be appreciated.
  #2 (permalink)  
Old 07-08-2004
beilstwh beilstwh is offline
Registered User
  
 

Join Date: Jul 2004
Location: New York State
Posts: 65
Never mind. with different search parameters, I found the answer.
use a combination of head and tail will return any range you want. Sorry to bug the forum, I should have looked harder first.

For example to get lines 1001-2000 your would issue the following command.

set -A CustNo `cat RAGEFF.lst|head -2000|tail -1000|cut -f1-19 -d',' -s |sed 's/,/ /g'|sed 's/\L//g'|nawk '{ if (NF == 19) {print $1}}' `

  #3 (permalink)  
Old 07-08-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
You could also pipe though sed to extract the range - e.g.

blah | sed -n '1,1000p' | blah

to extract the first 1000 lines of the file

cheers
ZB
  #4 (permalink)  
Old 07-08-2004
beilstwh beilstwh is offline
Registered User
  
 

Join Date: Jul 2004
Location: New York State
Posts: 65
I like the sed solution better then head and tail.

Thanks.
  #5 (permalink)  
Old 07-09-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Since you are already using awk, you can use it to specify a range....

nawk -F, 'NR>1000 && NR<=2000 && NF==19 {print $1}'
  #6 (permalink)  
Old 07-09-2004
beilstwh beilstwh is offline
Registered User
  
 

Join Date: Jul 2004
Location: New York State
Posts: 65
Even better!!

Thank you very much
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0