Click here to get back home

How Do I Locate a value in a text file and evaluate it and then write out that line based on the value?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How Do I Locate a value in a text file and evaluate it and then write out that line based on the value? kwalike57 12-28-2007
Posted by kwalike57 on December 28, 2007, 1:11 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Posted by Jürgen Exner on December 28, 2007, 1:48 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Posted by kwalike57 on January 2, 2008, 4:48 pm
Please log in for more thread options
> >I need to use Perl to read through log output from a df -k command on
> >a Unix server, for each line in the log where the value for %Used is
> >greater than 60% I need to write that line of the log out to another
> >text file. =A0The header %Used is located on the line above the actual
> >value, like this:
>
> >%Used
> >60%
>
> Where's the problem? Loop through the file using a standard while(){}.
> When you find a line with a content that is eq()ual to '%Used' then read t=
he
> next line and check if the value is > 60.
>
> >How would I capture and evaluate only the %Used value and no other
> >values in that line?
>
> That depends on what that line actually contains.
> Standard answer: if you know the separator between values, then use split(=
),
> if you know the value then use m/(...)/.
>
> It is impossible to give more detailed suggestions without seeing the actu=
al
> data.
>
> >The 60% occurs first in the value line followed by a percent value for
> >inodes used.
>
> That is nice, but a vague verbal description of the data can only result i=
n
> a vague verbal description of the algorithm.
>
> jue

Hello.

Thanks for the note.

Here is additional information:

Sample file excerpt...

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 131072 38912 71% 4261 32% /
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/inputlv 255328256 118925432 54% 564 1% /IPW/input

=46rom this example, I want to evaluate the column labeled %Used for
values greater than 60% and if that value is greater than 60%, I then
want to write that entire line, (/dev/hd4 131072 38912
71% 4261 32% /
) out to a text file that will be emailed out to report on filesystems
that are now at a capacity of 60% or greater.

I already have the script written to create the original df -k output
to a text file and create the email generation. I just need to have
help evaluating the correct values in the original df -k output file
and then creating the new file containing entries of all filesystems
that are at or above 60% capacity.

Does this help clarify my question?

Thanks again,

Karin Walike

Posted by Jürgen Exner on January 3, 2008, 1:20 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Posted by John W. Krahn on December 28, 2007, 2:06 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Similar ThreadsPosted
how to write text to a file December 17, 2006, 7:56 am
Can't write to a text file through CGI-Perl August 30, 2004, 9:52 am
Re: Can't write to a text file through CGI-Perl August 30, 2004, 10:01 am
perl script to write to a text file February 16, 2005, 4:27 pm
Parsing a text file line-by-line: skipping badly-formed lines? May 14, 2007, 11:10 am
Modify program to write just data to a text file. October 27, 2004, 3:35 am
Loop through a text file line by line January 12, 2005, 7:32 pm
Can't locate Text/CSV.pm in @INC (@INC contains: /usr/lib/perl5/5.8.8 June 15, 2006, 3:16 pm
Printing the next line of text of the file April 19, 2007, 6:37 pm
FAQ 5.2: How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? December 7, 2004, 6:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap