- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Print a specific line of a large text ascii file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2002 08:18 PM
07-18-2002 08:18 PM
Print a specific line of a large text ascii file
Is there any QUICK method in command line to print a specific line of a large ascii file (over 4M rows) ?
Regards,
Patrick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2002 08:22 PM
07-18-2002 08:22 PM
Re: Print a specific line of a large text ascii file
1) With the line number:
# cat -n $FILE | cut -c5- | grep "^$LINENO "
2) Without the line number but with the specific contents:
# grep $PATTERN $FILE
To identify the line number after from the grep:
# grep -n $PATTERN $FILE
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2002 08:25 PM
07-18-2002 08:25 PM
Re: Print a specific line of a large text ascii file
1) With the line number, you can also do this:
# head -$LINENO $FILE | tail -1
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 12:01 AM
07-19-2002 12:01 AM
Re: Print a specific line of a large text ascii file
sed -n '1325p' /your/large/file
e.g. (with a not really big file)
# cat -n /etc/services |sed -n '123p'
123 hacl-hb 5300/tcp # High Availability (HA) Cluster he
artbeat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 12:04 AM
07-19-2002 12:04 AM
Re: Print a specific line of a large text ascii file
# perl -ne 'print if $.==123' /etc/services
hacl-hb 5300/tcp # High Availability (HA) Cluster heartbeat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 12:14 AM
07-19-2002 12:14 AM
Re: Print a specific line of a large text ascii file
if like the "old beasts".... Here is a solution with awk:
awk 'NR=1234, NR=1234' input_file
This will only print the line with number 1234.
Allways stay on the bright side of life!
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 12:35 AM
07-19-2002 12:35 AM
Re: Print a specific line of a large text ascii file
more +/pattern file #Starts 2 lines above search string
more +linenumber file #Starts 2 lines above desired linenumber
See for more details/options --> man more
BB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 01:01 AM
07-19-2002 01:01 AM
Re: Print a specific line of a large text ascii file
if you need to print only one line with a specific pattern in it:
awk '/your_pattern/, /your_pattern/' input_file
Or what exactly do you want?
Allways stay on the bright side of life!
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 01:02 AM
07-19-2002 01:02 AM
Re: Print a specific line of a large text ascii file
don't forget another infamous beast:
$ echo "100p"|ed - my_file
Best regards
Juan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 06:36 AM
07-19-2002 06:36 AM
Re: Print a specific line of a large text ascii file
I noticed no one got the bunny, is there something else you are looking for?
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 07:03 AM
07-19-2002 07:03 AM
Re: Print a specific line of a large text ascii file
to get a specific line either u should know some pattern in the line or line number. but better go by line number because pattern may match with some other lines also.
by line number 23
#cat -n
if u want to print
#cat -n
it goes to default printer.
regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2002 07:10 AM
07-19-2002 07:10 AM
Re: Print a specific line of a large text ascii file
# perl -ne '123 .. 256 && print'
to print lines 123 through 256
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2002 05:31 PM
07-21-2002 05:31 PM
Re: Print a specific line of a large text ascii file
The reason why no one get the full point is that I have tried all the methods above before I post this question. I only want to seek anyone's help whether they have any other QUICK method to do that as all the methods above take a number of seconds to process.
Anyway, I thank all of the above experts to share their ideas.
Many thanks and regards,
Patrick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 02:14 AM
07-22-2002 02:14 AM
Re: Print a specific line of a large text ascii file
For an unstructured ASCII text file, i.e. a file with lines of *variable* length records/lines, you will have to read the file *sequentially* from the beginning to the desired line, i.e. it will be 'fast' for low line numbers and 'slow' for high line numbers.
Just *reading* a file of "4M rows" (How big, i.e. in MBytes, is the file?) will already take several seconds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 05:34 AM
07-22-2002 05:34 AM
Re: Print a specific line of a large text ascii file
That way if someone asks for a specific line in the text file, you could develop a program that could use the "seek" i/o function to position directly to that spot and begin reading. In this way you would only read through the entire file once to build the index.
I did this once a few years ago, but I can't find the program I developed.
Sometimes there is a trade off in your time developing a solution versuses the payback some other user will get.
Hope that Helps...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 06:03 AM
07-22-2002 06:03 AM
Re: Print a specific line of a large text ascii file
a5:/tmp 138 > pr -t -n3 xx.c
1 #include
2 #define FARTHING 0.25
3
4 int main ()
5 {
6 double farthing = FARTHING;
7 double nutherfarthing = 0.25;
8 printf ("%g\n", farthing);
9 printf ("%g\n", nutherfarthing);
10 return 0;
11 } /* main */
a5:/tmp 139 > perl mk_index.pl xx.c
a5:/tmp 140 > perl ls_index.pl xx.c
Line Offset
------- ----------
1 0
2 19
3 41
4 42
5 54
6 56
7 94
8 128
9 159
10 196
11 210
a5:/tmp 141 > perl rd_index.pl xx.c 4 2 7
int main ()
#define FARTHING 0.25
double nutherfarthing = 0.25;
a5:/tmp 142 >
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 06:29 AM
07-22-2002 06:29 AM
Re: Print a specific line of a large text ascii file
That's the right idea, but I think the index can get quite large if their are a lot of lines.
I was thinking of an index that had the block number (block being user defined, ie 4096 bytes) as the grouping and index value would be the line number that that blocks begins with. Then you could begin reading there until you reach the desired line number.
eg
block ... line#
0 1
1 359
2 782
3 1202
...
This way if you wanted line 837, you would seek to beginning of block 2 and read until you got to 837.
Since line numbers don't necessarily cross evenly over the fixed block size, if you wanted line 782, you would have to start at block 1 and count up.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 06:40 AM
07-22-2002 06:40 AM
Re: Print a specific line of a large text ascii file
Another point in my/your approch is the support for 64bit integers. If files get indexed past 64bit offsetts, my scripts will only work if both perl and DB_File support those large numbers.
Disk size can be minimized by using pack instead of just storing the plain number, which might also be much faster. If this were production, I'd for sure also make the key (line number) a packed integer, rather than the integer itself, causing a better distribution and all keys of the same length.
Whatever, it was just a quick hack to see if it would work. And it does. It's up to the one to use it how to optimize the process to her/his needs. This would also include the question of how static $big_file is, and if it changes, how fast it could/should be re-indexed.
You can even use a database like MySQL or progress to do it :P
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2002 07:59 AM
07-22-2002 07:59 AM
Re: Print a specific line of a large text ascii file
--8<--- mk_index.pl snippet
open FILE, "< $big_file" or die "$big_file: $!";
my ($pos, $line, @idx) = (0, 1);
tie @idx, "DB_File", $index, O_CREATE|O_TRUNCATE|O_RDWR, 0644, $DB_RECNO;
while (
$idx[$line++] = pack "L!", $pos;
$pos = tell;
}
-->8---
only if you have a more recent DB_File (with $DB_RECNO support) and native 64bit longs.
I'll leave the changes to the other files up to the reader