Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

changing a portion of a line leaving its formatting intact

by bronto (Priest)
on Dec 14, 2004 at 15:16 UTC ( #414721=perlquestion: print w/ replies, xml ) Need Help??
bronto has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks

I am building an application that should read and collect data from many NetApp filers and store them in a database for making periodical usage reports. To avoid bothering production filers during the early stage of development I created a file for each filer containing the output of the quota report command.

The output looks like:

K-Bytes Files Type ID Volume Tree Used Limit Used Limit Quo +ta Specifier ----- -------- -------- -------- -------- -------- ------- ------- --- +------------ tree 7 data pre_appltop 3449512 4194304 30723 +- /vol/data/pre_appltop tree 6 data pre_appsdata 23327540 31457280 142938 + - /vol/data/pre_appsdata

To simulate the command output, I'd need to read the file line by line, print if it doesn't begin with tree, otherwise substitute the fifth field (say: $field[4] with int rand $field[4]). Since the dashed line on top of the report gave me an hint for creating a sprintf format, I found that this one-liner just does the job I need:

perl -pe 'next unless /^tree/ ; @f=split ; $f[4] = int rand $f[4] ; $_ = sprintf "%-5s %8d %-8s %-8s %8d %8d %7d %7d %-15s\n",@f' filer.quota.report

But what if I hadn't any hint? How would you replace the fourth field leaving the line formatting in a sane state?

Thanks in advance for your suggestions

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.

Comment on changing a portion of a line leaving its formatting intact
Select or Download Code
Re: changing a portion of a line leaving its formatting intact
by Fletch (Chancellor) on Dec 14, 2004 at 15:33 UTC

    Not a direct answer to your question, but consult perldoc perlrun for the -a flag which you've inadvertently recreated.

Use a template
by redhotpenguin (Deacon) on Dec 14, 2004 at 15:54 UTC
Re: changing a portion of a line leaving its formatting intact
by ikegami (Pope) on Dec 14, 2004 at 16:05 UTC

    If you want to match tree, but not tree2 (for example), try:
    next unless /^tree[ ]/;
    instead of
    next unless /^tree/;

    Replaces 5th field:

    s/^(\S+[ ]+\S+[ ]+\S+[ ]+\S+[ ]+)(\S+[ ]+)/ my $s = int(rand($2)); $1 . $s . (' ' x (length($2) - length($s))) /e;

    Update: Added missing "$1 . ", as noticed by Animator. Removed unecesary if. Tested.

      shouldn't it be $1.$s?
Re: changing a portion of a line leaving its formatting intact
by Animator (Hermit) on Dec 14, 2004 at 16:06 UTC

    Why would this be a problem?

    You can replace $f[4] with with int($f[4]) . " " x (length $f[4] - length int($f[4]));

    Or you could use a regex instead of split...

    Regex you could use:

    Update: forgot length, added a possible regex

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2014-10-30 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls