Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW


by bigbossman72 (Initiate)
on Dec 16, 2003 at 16:11 UTC ( #315077=perlquestion: print w/ replies, xml ) Need Help??
bigbossman72 has asked for the wisdom of the Perl Monks concerning the following question:

hello -

i have been using this command to modify flat files that list directory contents, replacing the permissions listing with carriage returns:

/usr/bin/awk '{gsub(/lrw......./,"\n"); print}'
it works fine *except* on flat files that have more than 199 records (awk's limit, it turns out).

any idea how to do this with perl so as to avoid any record amount limit?

thanks in advance! - j

here is a sample flat file (ugly, isn't it?):

lrwxr-xr-x 1 root sys 8 Mar 13 2003 ./FESC_ADOT/runtime -> 01.30.02 lr +wxr-xr-x 1 root sys 8 Jul 30 2001 ./FESC_Websphere/runtime -> 01.00.0 +1 lrwxr-xr-x 1 root sys 8 Nov 24 16:22 ./qmc/runtime -> 01.50.01 lrw +xr-xr-x 1 root sys 9 Jul 7 11:50 ./ADOT_WEB/runtime -> web.1.18A lrwx +r-xr-x 1 root sys 18 Aug 20 2001 ./POSLITE/runtime -> POS_OPLITE01.01 +.05 lrwxr-xr-x 1 root sys 18 Aug 21 2001 ./MHSLITE/runtime -> MHS_OPL +ITE01.00.01 lrwxr-xr-x 1 root sys 16 Apr 15 2002 ./VAC/runtime -> 01. +01.03.regular lrwxr-xr-x 1 root sys 20 Aug 21 2001 ./BBSSLITE/runtime + -> BBSS_OPLITER01.01.01 lrwxr-xr-x 1 root sys 10 Feb 12 2002 ./Talar +ian/runtime -> 5.5R5/11.0 lrwxr-xr-x 1 root sys 19 Aug 21 2001 ./FESC +LITE/runtime -> FESC_OPLITE01.01.01 lrwxr-xr-x 1 adot dba 8 Aug 23 20 +02 ./TALLITE/runtime -> 01.00.04 lrwxr-xr-x 1 root sys 4 May 23 2002 +./PTTPLITE/runtime -> test lrwxr-xr-x 1 root sys 8 Nov 24 16:15 ./per +l/runtime -> 01.20.04

update (broquaint): tidied up formatting + added <readmore> tag

Replies are listed 'Best First'.
Re: awk/perl?
by duff (Vicar) on Dec 16, 2003 at 16:21 UTC
    It looks like you want
    /usr/bin/perl -pe 's/lrw......./\n/g;'
    it works fine *except* on flat files that have more than 199 records (awk's limit, it turns out).

    Somehow I doubt that. If awk only worked on files of less than 200 records, that would severely limit its utility. Perhaps your particular version of awk is crippled?

      i think awk is limited to the records in one line, but can process as many lines as you whant.

      For interest sake, from the options section of the gawk manpage:

      -mf NNN -mr NNN Set various memory limits to the value NNN. The f flag sets the maximum number of fields, and the r flag sets the maximum record size. These two flags and the -m option are from the Bell Labs research version of UNIX awk. They are ignored by gawk, since gawk has no pre-defined limits.
Re: awk/perl?
by Paladin (Priest) on Dec 16, 2003 at 17:09 UTC
    duff's and b10m's answers are what you are looking for for this job, but if something similar comes up in the future, you might want to look at the a2p program that comes with Perl.
    [~]$ echo '{gsub(/lrw......./,"\n"); print}' | a2p #!/opt/perl/bin/perl eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible) eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift; # process any FOO=bar switches $, = ' '; # set output field separator $\ = "\n"; # set output record separator while (<>) { chomp; # strip record separator s/lrw......./\n/g; print $_; }

    Basicly it takes an awk script, and turns it into a Perl script. It doesn't do as good of a job as a human programmer would, but gives you a good starting point to go from.

    You can get more info about it from the a2p man page.

Re: awk/perl?
by b10m (Vicar) on Dec 16, 2003 at 16:22 UTC
    You can do basically the same with perl
    open (INPUT, "<$flatfile"); while(<INPUT>) { $_ =~ /[dlrwxsS-]{10}\s/\n/g; print; } close INPUT;
    This snippet will substitute the (for example) "lrwxr-xr-x" part into a \n character. I took [dlrwxsS-] to match the possible characters. I might have forgotten one or two possibilities ;)
Re: awk/perl?
by bigbossman72 (Initiate) on Dec 16, 2003 at 19:42 UTC
    thanks all for your help. - j

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://315077]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2016-07-27 18:33 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (246 votes). Check out past polls.