- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl: file open / line modify... kept me up late..
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
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
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
тАО11-28-2001 12:26 AM
тАО11-28-2001 12:26 AM
perl: file open / line modify... kept me up late..
I've the followin excerpt of a perl script, that is meant to read and modify data a file.
but it doesn't work of course..
#
# FILE originally contains following line
# one|two|three|0|1
# $q is one
#
open(FILE, ">$questionfile");
@correct = grep(/^\Q$q|\E/,
close(FILE);
#
# FILE ends up blank
# should end up
# one|two|three|1|1
#
$q_line = $correct[0];
($aaa,$bbb,$ccc,$ddd,$eee) = split(/\|/, $q_line);
$fff =$ddd + 1;
$newline = "$aaa|$bbb|$ccc|$fff|$eee\n";
open(TMP, ">$tmp");
print TMP "$newline\n";
# ends up |||1|
close(TMP);
more or less, if there's a file that looks like this:
one|two|three|0|1
une|deux|trois|0|4
aon|do|tri|0|5
I want it to end up looking like this:
one|two|three|0|1
une|deux|trois|1|4
aon|do|tri|0|5
if the variable to search for, one, une, aon
is known and $q above..
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2001 06:41 AM
тАО11-28-2001 06:41 AM
Re: perl: file open / line modify... kept me up late..
Your first file, $question file, is open for output ">$question". You need the open the file for input "<$question".
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2001 07:33 AM
тАО11-28-2001 07:33 AM
Re: perl: file open / line modify... kept me up late..
@correct=grep...
Input lines from files have the \n on them, sometimes they can be ignored, but usually you don't want them.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2001 07:40 AM
тАО11-28-2001 07:40 AM
Re: perl: file open / line modify... kept me up late..
your problem is your grep statement. What are you looking for, because your grep is returning null??
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2001 11:00 AM
тАО11-28-2001 11:00 AM
Re: perl: file open / line modify... kept me up late..
what are you trying to match on here:
@correct = grep(/^\Q$q|\E/,
What is the "\Q" and "\E" for??
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2001 01:46 PM
тАО11-28-2001 01:46 PM
Re: perl: file open / line modify... kept me up late..
Just two points:
1. It appears to me that you need an exact match to the first column. Therefore, it is not a candidate for regex usage.
Use the eq operator instead:
@correct = grep $q eq $_,
You can include or omit the parens, Perl doesn't care:)
2. If all you need done is stated in your question, why not make use of Perl powerful command line interface:
perl -nalF'\|' -e "if (\$F[0] eq '$q') {\$F[3] +=1; print join '|', @F}" origfile > newfile
Assuming you have a shell variable $q set to somevalue.
Leslie