Multiplying column in awk and PHP


 
Thread Tools Search this Thread
Top Forums Programming Multiplying column in awk and PHP
# 1  
Old 03-07-2012
Multiplying column in awk and PHP

hello,

I'm writing a php script in fedora to run with a csv file. I want the script to read column 4 and multiply each single line in the column by 1000, how would that script look? I've written one script but it's obviously incorrect because it will not execute the command.

here is my script

Code:
<?php

$fileName_$SERVER["argv"][1];
echo "file: $filename\n";
$fp = fopen("$filename","r")or exit("cannot open or read file\n");

while(!feof($fp)). "\n";
    {
     echo fgets($fp). "\n";
     }
     fclose($fp);
      {

//This is where the problem comes in below

awk -F  '(($4=$4*1000); print $4\t)';
        }

Thanks,
Brandon

Last edited by brandonadam; 03-07-2012 at 04:37 PM..
# 2  
Old 03-07-2012
Welcome to the forums. I've moved your post to the programming forum where it's more appropriate(PHP is not shell programming) and will see more attention. In the future please post new questions as new threads.

PHP is not shell, you cannot run awk there in that way. You could run it with popen() and feed data into it, but I'm not entirely sure what you're trying to feed into it yet, so it's difficult to illustrate how.

Is this script being run through CGI, or locally? I'm not sure what I'm looking at. It's very odd to see $SERVER being used at the same time as ARGV arguments, since CGI generally doesn't have commandline arguments at all, just stdin and env variables...
# 3  
Old 03-07-2012
This is local, I'm trying to run this via the command line. The csv file i've written looks similar to this...

Tom, Jack, seats, 45, 115
Donna, Tom, seats, 55,56
ect..

The initial php script gave me the output that I wanted. What I'm wanting to do is have the script read the fourth column and multiply each line by 1000 and have it display on the command line. Would make it wayyy easier than going in and changing the original code, since there are about 500 or so lines. oh, and i'm trying to make them tab delimited files so I can get that space between each line, that is the reason behind the '\t'
# 4  
Old 03-07-2012
Using awk here would be quite awkward. (Pardon the pun.) It would be much better to either

1) convert this inside PHP itself

Code:
while($str=fgets($fp))
{
        $arr=explode(",", $str);
        $arr[3]=sprintf("%d", intval($arr[3])*1000);
        print implode("\t", $arr), "\n";
}

or

2) use awk externally, as part of a pipe chain.

Code:
php myprogram.php filename | awk -F, -v OFS="\t" '{ $4 *= 1000 } 1'

# 5  
Old 03-07-2012
ya that works out much better than I was doing..Smilie

..thanks alot for your help, I'm definitely happy with the outcome.

~Brandon
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

5. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

6. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

7. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

9. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

10. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies
Login or Register to Ask a Question