Visit Our UNIX and Linux User Community


Any solution with awk for volatile columns??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Any solution with awk for volatile columns??
# 1  
Old 02-18-2013
Any solution with awk for volatile columns??

Hi

I have this file with content

Code:
Code:
ale,4   ,ale,2  ,ale,1  ,ale,2
ale,1   ,ale,7  ,ale,7  ,ale,13
ale,6   ,ale,1  ,ale,1  ,ale,1
ale,1   ,ale,1  ,ale,37 ,ale,1
ale,1   ,ale,1  ,ale,2  ,ale,37
ale,77  ,ale,1  ,ale,53 ,ale,3
ale,5   ,ale,1  ,ale,2  ,ale,40
ale,1   ,ale,1  ,ale,44 ,ale,1
ale,117 ,ale,4  ,ale,1  ,ale,1
ale,2   ,ale,1  ,ale,5  ,ale,2


I wanted first column to be selected by default and den all the numeral columns i.e. column 2,4,6,8.

Major issue is the columns are volatile....
i.e. everyday there is one more script of mine which will add columns i.e. paste columns...
plzzzz help!!!!
# 2  
Old 02-18-2013
Can you give example on how you like the output.
Why not edit the original script that create this file
Quote:
script of mine
to give it output you like directly?
# 3  
Old 02-18-2013
Code:
Code:
ale,4   ,2      ,1      ,2
ale,1   ,7      ,7      ,13
ale,6   ,1      ,1      ,1
ale,1   ,1      ,37     ,1
ale,1   ,1      ,2      ,37
ale,77  ,1      ,53     ,3
ale,5   ,1      ,2      ,40
ale,1   ,1      ,44     ,1
ale,117 ,4      ,1      ,1
ale,2   ,1      ,5      ,2

Above mentioned is the required o/p...
one more thing is the columns are volatile dey are not fixed.....
i.e i wan the 1st column by default and den i want all the even columns i.e 2,4,6,8..... goes on
# 4  
Old 02-18-2013
Code:
awk -F, '{ s=$1;for(i=2;i<=NF;i++){ s=($i~/^[0-9]+/)?s FS $i:s; } print s }' filename

Or for even columns:
Code:
awk -F, '{ s=$1;for(i=2;i<=NF;i+=2) s=s FS $i; print s }' filename

# 5  
Old 02-18-2013
Bipin thanks for that.... once i executed ur awk script den i realised i had one more problem..... :P i.e. there are chances few times that the column just gives blank value not 0.... so cani replace those columns with blank value as 0....??
# 6  
Old 02-18-2013
Code:
awk -F, '{ s=$1;for(i=2;i<=NF;i+=2) s=($i~/^[ ]*$/)?s FS 0:s FS $i; print s }' filename

This User Gave Thanks to Yoda For This Post:
# 7  
Old 02-18-2013
Bipin,

It dint worked... i mean to say all my columns still have different no of lines

Code:
Code:
ale,421    ,3      ,3      ,4
ale,78     ,1      ,6      ,2
ale,684    ,1      ,3      ,1
ale,26     ,2      ,5      ,2
ale,13     ,1      ,1      ,5
ale,2      ,5      ,5      ,3
ale,8              ,2      ,207
ale,1              ,2      ,6
ale,5              ,253    ,6
ale,4              ,13     ,3
ale,1              ,3      ,33
ale,2              ,1      ,1
ale,4              ,1      ,4
ale,3              ,31     ,1
ale,3              ,4      ,112
ale,2              ,1      ,1
ale,539            ,5      ,1
ale,7              ,1      ,1
ale,7              ,1      ,1
ale,5              ,136    ,1
ale,76             ,4      ,1
ale,2              ,1      ,1
ale,1              ,1      ,158
ale,7              ,1      ,27
ale,1              ,1      ,246
ale,1              ,1      ,13
ale,1              ,2      ,4
ale,1              ,153    ,1
ale,1              ,43     ,2
ale,1              ,284    ,2
ale,123            ,13     ,5
ale,1              ,1      ,1
ale,2              ,1      ,5
ale,1              ,4      ,1
ale,1              ,4      ,1
ale,1              ,2      ,3
ale,1              ,2      ,196
ale,3              ,2      ,5
ale,2              ,5      ,2
ale,1              ,1      ,2
ale,1              ,195    ,26
ale,328            ,9      ,1
ale,31             ,5      ,4
ale,493            ,3
ale,8              ,30
ale,10             ,1
ale,3              ,5
ale,1              ,1
ale,5              ,1
ale,1
ale,2
ale,1

file after the awk script given by u...

Previous Thread | Next Thread
Test Your Knowledge in Computers #773
Difficulty: Medium
A finite-state machine (FSM) is a specialized computer developed by AT&T.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with awk solution to add columns

Hi all. Wondering if someone can help with an awk solution to a problem I'm stumped with. I have a matrix file with >1000 fields and would like to add another column after each column with a text label. For example: Input: $cat file.txt name col1 col2 col3 coln aaaa ... (2 Replies)
Discussion started by: torchij
2 Replies

2. Shell Programming and Scripting

sed or awk Solution

Hi I am having a csv file like this ahsh,90.82,add,32424,ahha hhdh,98.89,hdhdh,92728,neha hshs,you,97.7,hdhdhd,are,a jsjsj,wonderful,9788,79.9,aheh ahdh,95.5,girl, 2737,jolllI need to add width="100" to the value which is greater than 90 like decimal points but less than 100 Output... (5 Replies)
Discussion started by: kshitij
5 Replies

3. UNIX for Dummies Questions & Answers

Help with solution to add together columns of large file

Hi everyone. I have a file with ~500 columns and I would like to perform a simple calculation on every two columns. The file looks like this: $cat input id A B C D E F.....X 1 2 4 2 3 4 1 n 2 4 6 4 6 4 5 n 3 4 7 5 2 2 3 n 4 ... (5 Replies)
Discussion started by: torchij
5 Replies

4. Shell Programming and Scripting

Awk solution

Hello! Well, I searched and wasn't able to find a specific example of my dilemma, so hopefully someone could assist? Or maybe there was an example but I missed it? I have two files: file1 = order data file file2 = list of 65,000+ order numbers I would like to extract from 'file1' any... (5 Replies)
Discussion started by: rm -r *
5 Replies

5. AIX

mkinstallp mark a file as volatile

I want to make a bff installp package which should be AIX5.2 compatible, but i don't know how to mark a file as volatile within the mkinstallp's template file. Anyone could help pls? (1 Reply)
Discussion started by: acerlinux
1 Replies

6. Shell Programming and Scripting

AWK solution to subtract multiple columns of numbers

Hope somebody is happy. NR==1 { num_columns=split( $0, menuend ); next; } { split( $0, substrend ); for ( i=1; i<=NF; i++ ) { minuend -= substrend; } } END { print "Result:"; for ( i=1; i<=num_columns; i++ ) { printf(... (3 Replies)
Discussion started by: awkward
3 Replies

7. Shell Programming and Scripting

Is there a awk solution for this??

I am writing a awk script that gathers certain data from certain fields. I needed a awk solution for this, because it will later become a function in the script. I have the following data that I need output on a single line, but record spans across multilple lines and records are not... (7 Replies)
Discussion started by: timj123
7 Replies

8. Shell Programming and Scripting

using awk for volatile column list

Hi guys I have my 5th column like this 12 12:00 12:13:14 5-12:13:14 Meaning 5 days 12 hrs 13 minutes and 14 seconds I need to split this column into above meaning format. But all other columns are separated by single while space. I am using awk command to print other... (8 Replies)
Discussion started by: yorker
8 Replies

9. Programming

volatile variable

give me the detail about volatile , applications, how to use volatile variable (2 Replies)
Discussion started by: vinodkumar
2 Replies

10. Programming

volatile

hi plz explain about volatile keyword in C language bye... (1 Reply)
Discussion started by: venkat_t
1 Replies

Featured Tech Videos