Replace values in columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values in columns
# 1  
Old 10-16-2014
Replace values in columns

I have the following input format:

Code:
AA00000712000  -0.17   0.90  -1.04  -0.37  -1.45  -1.13  -0.22  -0.34  -0.55   2.37   0.67  -0.48 -0.48
AA00000712001   0.15   0.03   0.47   0.62   2.04   1.14   0.29  -0.81   0.85   0.53   1.00  -0.10 -0.48
BB00000712000   1.32  -0.47   0.44   0.00   0.98  -1.17  -1.03  -0.53   0.17   0.43   0.83   2.65 -99.99
BB00000712001   1.32  -0.47   0.44   0.00   0.98  -1.17  -1.03  -0.53   0.17   0.43   0.83   2.65 -0.48

I need to be able to search for "BB" and replace each value in fields $2 through $13 with -99.99.

The following code will only perform this for 1 field at a time. I need to do all 13 fields at one time.
Code:
awk '$1 ~ /BB/ {$2 = -99.99; for (i=2; i<=NF; i++) $i=sprintf("%6.2f", $i)}1' input > output

Any help is appreciated. Thanks.
# 2  
Old 10-16-2014
awk '{ if ($1 == "BB*") $2=-99.99;$3=-99.99;$4=-99.99; print $0 }' input
# 3  
Old 10-16-2014
Just a slight modification.
Code:
awk '$1 ~ /BB/ {for (i=2; i<=13; i++) { $i = sprintf("%6.2f", -99.99) }}1'

Let's edit the NF for 13 since you have more than 13 fields
This User Gave Thanks to Aia For This Post:
# 4  
Old 10-16-2014
Perl if interested

Code:
perl -wlape 'if($F[0] =~ /BB/){ map {$_ = -99.99} @F[1..$#F]; $_ = "@F"}' file

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 10-16-2014
Code:
perl -wlape 'if($F[0] =~ /BB/){ map {$_ = sprintf "%6.2f",-99.99} @F[1..12]; $_ = "@F";}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

2. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

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... (12 Replies)
Discussion started by: as7951
12 Replies

3. Shell Programming and Scripting

Multiple columns replace with null values.

I am trying to replace the partcular columns(Col3,col5,col20,col44,col55,co56,col59,col60,col61,col62,col74,col75,col88,col90,col91,col93,col94,col95) with empty Input file Col1,col2,col3,col4,col5------,col100 1,2,3,4,5,---------,100 3,4,5,6,7,---------,300 Output : ... (3 Replies)
Discussion started by: onesuri
3 Replies

4. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

5. Shell Programming and Scripting

Match on columns and replace other columns

Hi Friends, I have the following input file cat input chr1 100 200 0.1 0.2 na 1 na nd chr1 105 200 0.1 0.2 1 1 na 98 chr1 110 290 nf 1 na nd na 1 chr2 130 150 12 3 na 1 na 1 chr3 450 600 nf nf na 10 na nd chr4 300 330 1 1 10 11 23 34 My requirement is 1. If $6 is na make $7 nd and... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

Replace duplicate columns with values from first occurrence

I've a text file with below values viz. multiple rows with same values in column 3, 4 and 5, which need to be considered as duplicates. For all such cases, the rows from second occurrence onwards should be modified in a way that their values in first two columns are replaced with values as in first... (4 Replies)
Discussion started by: asyed
4 Replies

7. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

Get values from different columns from file2 when match values of file1

Hi everyone, I have file1 and file2 comma separated both. file1 is: Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9,Header10 Code7,,,,,,,,, Code5,,,,,,,,, Code3,,,,,,,,, Code9,,,,,,,,, Code2,,,,,,,,,file2... (17 Replies)
Discussion started by: cgkmal
17 Replies

9. Shell Programming and Scripting

Summing values in columns

Basically I have to process a text file which has been sorted this way: John 12 John 13 John 10 John 900 Peter 20 Peter 30 Peter 32 The first column is a name, and the second an arbitrary value, both delimited by a space. How can I sum them up such that it would become: John 935... (2 Replies)
Discussion started by: Dwee
2 Replies

10. UNIX for Dummies Questions & Answers

Replace columns from File1 with columns from File2

Hi all, I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command: awk 'NR==FNR{a=$1;b=$2;c=$3;next;} {$2=a;$4=b;$5=c;print}' file2 file1 > temp mv -f temp file1 First, i make the changes and save it as a temp... (1 Reply)
Discussion started by: seijihiko
1 Replies
Login or Register to Ask a Question