Because I know the Linux /etc/passwd file has exactly seven fields I can use this approach. Instead of reading each line into a Perl array, I treat it as a fixed set of variables. The only difference is the way I treat each line when I read it. This second Perl split example that shows how to process a text file with variable-length, delimited fields is almost identical to the first program. # read the fields in the current record into an = split(':', $_) Īs you can see from that code, each field on each line is split by the : character, and I read each line into the array, and then print the first field from each line with the $fields variable. Open(FILE, $filename) or die "Could not read from $filename, program halting." # nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false # purpose: read the /etc/passwd file, whose columns are separated by ':' ![]() This example shows several things, including how to split a record by the : character, which is the column delimiter in the Linux /etc/passwd file. In this first “Perl split” example program, I’ll read all of the fields in each record into an array named and then I’ll show how to print out the first field from each row. When processing these files, you can use the Perl split function, which I’ll demonstrate in two short programs here. Typically these files have variable-length fields and records, and the fields in each record are delimited by some special character, usually a : or | character. ![]() Many times you need a Perl script that can open a plain text file, and essentially treat that file as a database. ![]() Perl FAQ: How can I split a string in Perl, such as the strings in a pipe-delimited text file?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |