AWK - check column data

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support AWK - check column data
# 8  
Old 12-31-2009
Hello
Here you go
Code:
gaurav@localhost:~$ perl -n -e 'split(/\s+/,$_);foreach $i (@_){if($i!~/[a-z]/){print $i-0.1*$n{$i}," ";$n{$i}++}else{print $i," ";}}print "\n"' infile
1 e 1.2 1.6 
5 f 2.3 3.6 
3 g 1.1 2.6 
6 i 2.2 3.5 
8 o 1 3.4 
gaurav@localhost:~$

cheers ,
Happy New year.
Smilie
# 9  
Old 12-31-2009
It's not clear to me what should be the output for an input like this one:


Code:
1 e 1.2 1.6
5 f 1.6 1.2

This:

Code:
% perl -n -e 'split(/\s+/,$_);foreach $i (@_){if($i!~/[a-z]/){print $i-0.1*$n{$i}," ";$n{$i}++}else{print $i," ";}}print "\n"' infile
1 e 1.2 1.6 
5 f 1.5 1.1

Or this (unchanged);

Code:
% awk '{
  for (i=2; ++i<=NF;)
    c[i,$i] = f[i,$i]++ ? c[i,$i] += 0.1 : 0
  }
{
  for (i=2; ++i<=NF;)
    $i = sprintf("%.1f",$i -= c[i,$i])
  }
 9' infile
1 e 1.2 1.6
5 f 1.6 1.2

# 10  
Old 12-31-2009
Quote:
Originally Posted by radoulov
It's not clear to me what should be the output for an input like this one:


Code:
1 e 1.2 1.6
5 f 1.6 1.2

This:

Code:
% perl -n -e 'split(/\s+/,$_);foreach $i (@_){if($i!~/[a-z]/){print $i-0.1*$n{$i}," ";$n{$i}++}else{print $i," ";}}print "\n"' infile
1 e 1.2 1.6 
5 f 1.5 1.1

Or this (unchanged);

Code:
% awk '{
  for (i=2; ++i<=NF;)
    c[i,$i] = f[i,$i]++ ? c[i,$i] += 0.1 : 0
  }
{
  for (i=2; ++i<=NF;)
    $i = sprintf("%.1f",$i -= c[i,$i])
  }
 9' infile
1 e 1.2 1.6
5 f 1.6 1.2

I think he wants to reduce the value the no. of times it occurs * 0.1 . Image
Incidently the same value doesnt occur in the different columns in the dataset provided by him.
# 11  
Old 12-31-2009
Dear Radoulov,

Sorry,It's working fine.... Thanks a lot...

Please can u give a comment. how this program works..

Thanks
vasanth

---------- Post updated at 12:12 PM ---------- Previous update was at 12:04 PM ----------

Dear Radoulov,

If the input file contains more than 4 column, then the output, after fifth column prints as -0.0,-0.1,etc...

The other should be printed as it is..

Thanks in advance..
VAsanth
# 12  
Old 12-31-2009
Quote:
Originally Posted by vasanth.vadalur
[...]
If the input file contains more than 4 column, then the output, after fifth column prints as -0.0,-0.1,etc...

The other should be printed as it is..
This should fix it.

Code:
awk '{ 
  for (i=2; ++i<=4;)
    c[i,$i] = f[i,$i]++ ? c[i,$i] += 0.1 : 0 
  }
{ 
  for (i=2; ++i<=4;) 
    $i = sprintf("%.1f",$i -= c[i,$i])
  }
 9' infile

# 13  
Old 12-31-2009
Dear radoulov,

Yaa... Its worked fine...

Still if i need to check pairs of third column($3) and fourth column($4), with the next line upto end... wht will be the program..

Eg:

Input file :

w e 4.2 5
t j 5 6
y u 4.2 5
y i 5 5

output:


w e 4.2 5
t j 5 6
y u 4.1 5
y i 5 5

Thanks in advance

Last edited by vasanth.vadalur; 12-31-2009 at 05:01 PM..
# 14  
Old 12-31-2009
Code:
awk '$3 -= 0.1 * c[$3,$4]++' infile

Thanks to gaurav1086 for the algorithm, I'm not good at math Smilie

---------- Post updated at 10:22 PM ---------- Previous update was at 10:07 PM ----------

It should be:

Code:
awk '($3 -= 0.1 * c[$3,$4]++)||1' infile

Otherwise you'll loose the records where $3 = 0 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get row data printed in column using awk?

Hi team, I have below sample file. $ cat sample dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc structuralObjectClass: NphData objectClass: NphData objectClass: MSISDN entryDS: 0 nodeId: 35 createTimestamp: 20170216121047Z modifyTimestamp: 20170216121047Z... (3 Replies)
Discussion started by: shanul karim
3 Replies

2. Shell Programming and Scripting

Append data with substring of nth column fields using awk

Hi guys, I have problem to append new data at the end of each line of the files where it takes whole value of the nth column. My expected result i just want to take a specific value only. This new data is based on substring of 11th, 12th 13th column that has comma seperated value. My code: awk... (4 Replies)
Discussion started by: null7
4 Replies

3. UNIX for Dummies Questions & Answers

Using awk to find and use the maximum value in column of data

Dear Unix Gurus, I have a text file with multiple columns, for example, see sample.txt below 0 1 301 1 4 250 2 6 140 3 2 610 7 1 180I want to find the maximum in, say, column 3, normalise all the values to this maximum value (to 4 decimal places) and spit everything into a new... (2 Replies)
Discussion started by: tintin72
2 Replies

4. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Printing another column using awk and input data

Hi, I have data of the following type, chr1 234 678 39 852 638 abcd 7895 chr1 526 326 33 887 965 kilj 5849 Now, I would like to have something like this chr1 234 678 39 852 638 abcd 7895 <a href="http://unix.com/thread=chr1:234-678">Link</a> chr1 526 326 33 887 965 kilj 5849 <a... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

7. Shell Programming and Scripting

Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows. Random data Name-Number-ID Sarah-2.0-15 Bob-6.3-15 Sally-1.0-10 James-1.0-10 Scotty-10.7-15 So I would select all those who have ID = 15 and then add up total number read - p "Enter ID number" Num ... (3 Replies)
Discussion started by: Ironguru
3 Replies

8. Shell Programming and Scripting

Calculate data and make it into new column using awk

Hi everyone, just some simple question... i've been using a awk script to calculate my data... i have 3 files: file a1.txt: 2 3 4 5 3 4 file a2.txt: 4 5 6 7 8 (1 Reply)
Discussion started by: yat
1 Replies

9. Shell Programming and Scripting

two-column data to matrix in AWK

Howdy, I need to convert an association data matrix, currently in a two-column format, into a matrix with numbers indicating the number of associations. I've been looking around for AWK code in the list, but could not find anything. Here's an example of what I want to perform: original... (10 Replies)
Discussion started by: sramirez
10 Replies

10. Shell Programming and Scripting

How to check a column contain numeric or char data type ??

I have a file called clientname_filename.csv whose contents are like col1|col2|col3|col4| 510|abc|xxx|450| 510|abc11|yyy|350 510|pqr99|zzz| 670 512|222|439|110 Here i have check the contents of column for data type. i have a constraints that col1 always contain Numeric value column 2... (12 Replies)
Discussion started by: jambesh
12 Replies
Login or Register to Ask a Question