LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-08-2013, 11:50 AM   #31
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled

that worked!

thanks,

Tabby
 
Old 01-08-2013, 07:24 PM   #32
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Quote:
Originally Posted by ntubski View Post
Hmm, maybe I'm missing something obvious, but I think it's actually quite a good method (provided you create a unique filename with mktemp(1)). This is the same method used by sed -i actually:
Take for instance a counter stored in a file, and accessed constantly by a thousand plus users. If you rm and then move, then the file may not exist at the time that some other user makes a grab for it. Not that such a scenario is at play in this case. But even if the timing never works out to fail that condition, it is still posible for the timing to work out where two or more users perform the same update because they grabbed the same old data before performing an update. Which can be an issue when the counter is used to generate unique IDs. Lame example, but sort of my point. It's not always a simple single threaded, single user world these days.
 
Old 01-09-2013, 07:36 AM   #33
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by Shadow_7 View Post
Take for instance a counter stored in a file, and accessed constantly by a thousand plus users.
Okay, in that scenario temp+mv is not sufficient, but adding a rm in the middle just makes the bug harder to uncover. A solution for that would be flock(1) around temp+mv.
 
Old 01-09-2013, 07:47 AM   #34
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by Shadow_7 View Post
Take for instance a counter stored in a file, and accessed constantly by a thousand plus users. If you rm and then move, then the file may not exist at the time that some other user makes a grab for it. Not that such a scenario is at play in this case. But even if the timing never works out to fail that condition, it is still posible for the timing to work out where two or more users perform the same update because they grabbed the same old data before performing an update. Which can be an issue when the counter is used to generate unique IDs. Lame example, but sort of my point. It's not always a simple single threaded, single user world these days.
That calls for a shared memory segment instead.

And the use of semaphores to serialize access. It is not an application for file level locking, though you can do that to. Open the file read/write, then lock for exclusive access.

And no, don't use fopen. Use open - that way you don't get the conflict/race with the buffer usage.
 
Old 01-09-2013, 10:43 AM   #35
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
theNbomr

so this to me is really goofy, but sometimes the values in the 3rd column of my data are positive and sometimes they're negative idk??? but doesn't really matter why, and the same is true for the 4th column, again idk.

so i need to have an "if" statement in the script for each of column. so no matter how the data is given to me, i need all the values in the 3rd column to always be negative and all the values in the 4th column to always be positive.

so how do i include an if statement in there. i tried....

Code:
#! /usr/bin/perl -w
#
#  LQatjurhs.pl
#
#  Usage: LQatjuhrs.pl -i file.dat
#

use strict;

    while(<>){ 
        my @z=split; 
        print "$z[0], $z[1], $z[2], ",$z[3]*-1 if($z[3]>0 ,"$z[4]*-1 if($z[4]<0)\n";
    } 
    exit 0;
and of course that's wrong

thanks for your help, Tabby

Last edited by atjurhs; 01-09-2013 at 11:21 AM. Reason: clarify problem statement
 
Old 01-09-2013, 12:26 PM   #36
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is not clear whether you want to print only the values matching your criteria, or if you want to modify all of the values so that they do meet the citeria: "i need all the values in the 3rd column to always be negative and all the values in the 4th column to always be positive."

This prints only the ones that already match.

Code:
#! /usr/bin/perl -w
#
#  LQatjurhs.pl
#
#  Usage: perl -i LQatjuhrs.pl file.dat
#

use strict;

    while(<>){ 
        my @z=split;
        if( $z[2] gt 0 && $z[3] lt 0 ){
            print "$z[0], $z[1], $z[2], ",$z[47]*-1,"$z[4]\n";
        }
    } 
    exit 0;
--- rod.
 
Old 01-09-2013, 12:53 PM   #37
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
sorry, i need "or if you want to modify all of the values so that they do meet the citeria" then print them
 
Old 01-09-2013, 01:08 PM   #38
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
#! /usr/bin/perl -w
#
#  LQatjurhs.pl
#
#  Usage: perl -i LQatjuhrs.pl file.dat

use strict;

    while(<>){ 
        my @z=split;
        $z[2]  = abs( $z[2] ) * -1;
        $z[47] = abs( $z[47] );
        print "$z[0], $z[1], $z[2], $z[47], $z[4]\n";
    } 
    exit 0;
Where you refer to columns, this assumes you are talking about the columns in the output. The attempt in the previous post got that wrong.

--- rod.
 
Old 01-09-2013, 02:40 PM   #39
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
thanks!

just one other simple question, how come you represent the third column as
Code:
 $z[47]  instead of  $z[3]
is there a coding reason? i've just been using a 3

Last edited by atjurhs; 01-09-2013 at 02:42 PM.
 
Old 01-09-2013, 06:52 PM   #40
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your original Awk spec
Code:
awk -F " " '{print $1, $2, $3, $48*(-1), $5}'
used $48, which is the 48th field in the input. In Perl, when you split on whitespace, like Awk does by default, the fields are indexed starting at zero instead of one. That was the basis for my comment about distinguishing between the input columns and the output columns.

--- rod.

Last edited by theNbomr; 01-09-2013 at 06:53 PM.
 
Old 01-10-2013, 03:27 AM   #41
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Bear in mind that for things like awk the delimiter is a space. So if your filenames contain spaces, and you're trying to grab the whole filename, that could prove difficult. With various other quirks with quotes and commas and other things depending on what thing(s) your messing with at any given moment.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
[SOLVED] replace matched pattern from 1st file into 2nd file using awk sopier Programming 6 12-13-2011 09:58 AM
[SOLVED] Root unable to chmod, chown or operate on specific file chochem Linux - Software 2 09-06-2009 02:19 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 01:09 PM
How to operate socket with file interface wangjinyi Programming 4 11-27-2005 11:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration