Converting odd values to even values(or vice-versa) located in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting odd values to even values(or vice-versa) located in a column
# 1  
Old 10-09-2012
Converting odd values to even values(or vice-versa) located in a column

Hello All,

I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B.
Note that my data has some other columns(not shown here) too (around 100) after col2.
Code:
Tool,Data
A,1
A,3
A,5
....
so on
B,2
B,4
....

so on
I want to convert all odd numbers to even numbers( adding 1).. So my output is like below.

Code:
Tool,Data
A,2
A,4
A,6
.....
so on
B,2
B,4
.... 
so on

I need a one liner code for this without splitting the file into 2 portions and joining them later.
I tried tried like
Code:
awk -F, 'NR==1; $1=='A'' infile | awk -F, '$2=$2+1' >temp1.CSV
awk -F, '$1=='B'  infile >temp2.CSV
cat temp1.CSV temp2.CSV >outfile

But I want to avoid these temporary files temp*..

Thanks

Sidda
# 2  
Old 10-09-2012
Try:

Code:
awk -F, '$2%2{$2++}1' OFS=, infile > outfile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 10-09-2012
you can avoid temp files but not temp file.
you have to use atleast one temp file.

try

Code:
awk -F, '$1==A{$2+=1}1' OFS="," file > temp
mv temp file

This User Gave Thanks to pamu For This Post:
# 4  
Old 10-09-2012
Try this:
Code:
$ cat t
A,2
A,4
A,6
B,2
B,4

$ awk '{n = substr($0, match($0, /[0-9]+/), RLENGTH) + 1; sub(/[0-9]+/, n); print }' t
A,3
A,5
A,7
B,3
B,5

# 5  
Old 10-09-2012
Code:
perl -i.bak -F',' -ape 'if($F[0] eq "A") {$F[1]++;$_=join(",",@F)."\n"}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Column Values to a Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example: "Column A" 1 2 3 4 10 12 14 15 16 17 18 "Column B" 1 4 5 6 (4 Replies)
Discussion started by: newbio
4 Replies

2. Shell Programming and Scripting

Converting a binary file to ascii and vice versa?

Hi All, I have a binary file which is being exported from a Database, and i need to convert that to ASCII format. How can i achieve that? And this solution should work for any file which is given to us; means they will give different files from different tables. Thanks in advance. (8 Replies)
Discussion started by: baranisachin
8 Replies

3. Shell Programming and Scripting

Converting from Centigrade to Fahrenheit and vice versa.

I need to write a script that will take the input from a file and convert the number from centigrade to fahrenheit and vice versa. This is what I have but it doesn't seem to be correct. Also the data file has 11 numbers inside of it and the output needs to be listed as so: Fahrenheit Temperature... (18 Replies)
Discussion started by: N1ckNak
18 Replies

4. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

5. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

6. Shell Programming and Scripting

Converting values in a ROW to COLUMN

Hi All, I needd to convert values in a row to a column. eg: Input is as: value1,value2,value3,value4,.........,value N Required Output: Value1 Value2 Value3 . . . Value N Please help.... (3 Replies)
Discussion started by: sambaman
3 Replies

7. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

9. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies

10. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question

Featured Tech Videos