AWK help ! or others ways to do it!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK help ! or others ways to do it!
# 1  
Old 03-30-2011
AWK help ! or others ways to do it!

Hi!

I really need your help!

I need to operate the columns separate by ',' of a file with this structure

HTML Code:
a1,a2,a3
b1,b2,b3,b4,b5
c1,c2
d1,d2,d3
e1
....
and I want the result of this subtractions

HTML Code:
a1-a1,a2-a1,a3-a1
b1-b1,b2-b1,b3-b1,b4-b1,b5-b1
c1-c1,c2-c1
d1-d1,d2-d1,d3-d1
e1-e1
For example, if the file is

HTML Code:
43805414,43805643,43805660,43805688,43805771,43805788,43805876,
116842150,116844494
117072160,117072559,117073456
115850458,115850499,115650901,115651050
115538171,
The desired result would be

Code:
0,229,246,274,357,374,462
0,2344 
0,399,1296 
0,41 
0

I'm a beginer at awk, I tried this code

Code:
awk -F, '{print $1-$1, $2-$1, $3-$1, $4-$1, $5-$1, $6-$1, $7-$1}' ./InputFile

But the numbers of columns is variable, some lines has many columns and I don't want the negative results. Besides, if it possible, I want the output separate by ','

Thanks a lot!!!!!!!!!
# 2  
Old 03-30-2011
Code:
nawk -F, '{for(i=1;i<=NF;i++) printf("%s%c", $i-$1, (i==NF)?ORS:FS)}' myFile

What should be done with the negative numbers?
# 3  
Old 03-30-2011
Try:
Code:
awk -F',' '{for(i=1;i<NF;i++) {printf ($i>=$1)?$i-$1 FS:""};print ($NF>$1)?$NF-$1:""}'


Last edited by yinyuemi; 03-30-2011 at 06:50 PM..
# 4  
Old 03-30-2011
To skip negative results:

Code:
awk -F',' '{A="";for(i=NF;i;i--)if($i-$1>=0)A=($i-$1)(A?","A:""); print A}'

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-30-2011
Thanks you!

I tested both codes with this file:

Code:
1,2,3,4,5
2,5,6,45
3,56,100,1000
2,

with the vgersh99's code I get

Code:
splicing@splicing-desktop:~/awk$ nawk -F, '{for(i=1;i<=NF;i++) printf("%s%c", $i-$1, (i==NF)?ORS:FS)}' ./c12.2
0,1,2,3,4
0,3,4,43
0,53,97,997
0,-2

Almost perfect!! but I seems that when there are only 1 column, produce negative results Smilie...
Because the first column has the minor number, I don't expect negative results. If you give me some way to filter out the negative results, I will thanks you a lot!

with the yinyuemi's code I get

Code:
 
splicing@splicing-desktop:~/awk$ awk -F',' '{for(i=1;i<NF;i++) {printf $i-$1 FS};print $NF=$1}' ./c12.2
0,1,2,3,1
0,3,4,2
0,53,97,3
0,2

This is not what I need Smilie.
I need subtracts column 1 against the others columns. Thanks you anyway.
# 6  
Old 03-30-2011
Code:
nawk -F, '{for(i=1;i<=NF;i++) printf("%s%c", ($i=="")?"":$i-$1, (i==NF)?ORS:FS)}' myFile
# or to filter out ALL negative results
nawk -F, '{for(i=1;i<=NF;i++) printf("%s%c", ($i<$1)?"":$i-$1, (i==NF)?ORS:FS)}'

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 03-30-2011
Chubler_XL's code works perfect!

thanks you guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Different ways to check a file

to get the checksum of a file on unix systems, you can just use the "cksum" command. i discovered there are some watered down versions of unix systems i have to do some work on. surprisingly, these systems have perl installed on them and awk. so if the cksum command is not available on a... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Anyway different ways of doing this program

Okay so I'm 13 and my dad set me a challenge of writing a program that check to see if a box is online with input parameter when starting the program so i came back with this.The top commented out bit is what he showed me how to do after i show him my code. #!/bin/sh #in=${@} #tst=`echo... (3 Replies)
Discussion started by: mattylad000
3 Replies

3. Shell Programming and Scripting

Ways to run script from anywhere

Hi I am trying to make my scripts to run from any directory and after some research (googling) I find there are 2 ways to do this 1. export PATH=$PATH:directory 2. create a /bin directory in your home directory and put your scripts there. Could someone tell me the relative merits of either... (1 Reply)
Discussion started by: piynik
1 Replies

4. Programming

Sum a number in different ways.

Hello, I need some help to correct my algorithm steps to sum a number. I am confused... please input some steps Example: Let's say a number 5. Now we can get a list of numbers: 1, 2, 3, 4 (less than 5 in sorted order). so the sum could be: 1+4; 2+3 ( 1+1+1+1+1 or 1+2+2 is not a solution). ... (1 Reply)
Discussion started by: sureshcisco
1 Replies

5. UNIX for Dummies Questions & Answers

Different ways to get OS version

I am trying to figure out the OS version of my Linux box. I got three commands: # uname -a Linux test01 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 # cat /proc/version Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704... (4 Replies)
Discussion started by: insvf
4 Replies

6. UNIX for Dummies Questions & Answers

Different ways in which applications are Launched.

How does X windows system launch a program? Does X give init a command (fork) or does x give BASH a command? Can/does X interact with init directly or does everything go through BASH? :wall: Thanks in advance! (4 Replies)
Discussion started by: theKbStockpiler
4 Replies

7. What is on Your Mind?

10 e-ffective Ways to Get Backlinks!

Backlinks are super important on the web today! Without them your site most likely will not get found by search engine spiders. If your website has been generating low to No traffic then it is probably due to the lack of backlinks. Below are 10 e-ffective Ways to Get Backlinks! 1. Forum... (0 Replies)
Discussion started by: donaldcarter55
0 Replies

8. UNIX for Dummies Questions & Answers

Ways to know about a command.......

What are the ways to know about a command? (7 Replies)
Discussion started by: g.ashok
7 Replies

9. UNIX for Dummies Questions & Answers

ways to open a file

How many ways are there to open a file in unix? wha i know is mentioned below suppose if xx is a filename, then 1) cat xx 2) more xx 3) view xx 4) less xx I heard there are seven ways to open a file. If so, can anyone tell me all the ways to open a file and correct me the above... (3 Replies)
Discussion started by: venkatesht
3 Replies

10. Tips and Tutorials

12 Ways to Parse a file

A common thing in shell scripting. I came across this script that will be useful for people learning to write script. #!/usr/bin/ksh # # SCRIPT: 12_ways_to_parse.ksh.ksh # # # REV: 1.2.A # # PURPOSE: This script shows the different ways of reading # a file line by line. Again... (0 Replies)
Discussion started by: google
0 Replies
Login or Register to Ask a Question