Replace 0 with 1 in multiple fields with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace 0 with 1 in multiple fields with awk
# 1  
Old 01-27-2015
Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file:
Code:
1	3	3	2
3	3	4	0
4	0	5	4
5	2	2	0
5	3	4	0
6	0	3	2

I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's"

I tried the following, but it's not working

Code:
awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input

This code will work if I just specify one field, but it doesn't work if I use more than one: i.e. ($2==0) || ($4==).

Thanks in advance!
# 2  
Old 01-27-2015
Hello Rabu,

Could you please try following and let me know if this helps you.
Code:
awk '{$2=$2==0?1:$2;$4=$4==0?1:$4} 1' OFS="\t"  Input_file
OR
awk '{gsub("0","1",$2);gsub("0","1",$4)} 1' OFS="\t" Input_file

Output will be as follows.
Code:
1       3       3       2
3       3       4       1
4       1       5       4
5       2       2       1
5       3       4       1
6       1       3       2

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-27-2015 at 12:46 AM.. Reason: Added one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-27-2015
Hi Ravinder,

The code works great!

What does the "1" at the end of the code do?

Thanks,
Rabu
# 4  
Old 01-27-2015
Hello Rabu,

awk works on theme of awk '/pattern/{condition} {action} so here $2=$2==0?1:$2 will set $2 to 1 if it is zero and leave it as it is if it is a non zero value, same with $4 value.
Code:
$2==0(checking condition here for $2 if it's value equal to 0)
?    (when condition is true.)
1    (setting $2's value to 1)
:    (when condition is FALSE.)
$2   (keep the value of $2 as it is.)

After setting values for $2 and $4 now I have given there 1 means it makes the condition to TRUE now when condition is TRUE, awk's by default action is print if no action is specified as in this code.

Hope this helps, let me know if you have any queries on same.


Thanks,
R. Singh
# 5  
Old 01-27-2015
NB the gsub would change a 10 to 11.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 01-27-2015
Thanks Ravinder for the explanation!
And thanks for the heads up, MadeInGermany.

If my input file contains multiple fields (14), would there be any way of doing this for every even numbered field without having to put in $2, $4, $6, $8 etc...

Code:
1       3       3       2       4       2       4       0       2       0       2       0       4       0
3       3       4       0       5       0       2       0       3       0       3       4       4       3
4       0       5       4       3       2       2       3       2       3       4       4       2       4
5       2       2       0       2       2       2       3       2       3       3       2       1       2
5       3       4       0       6       0       9       4       9       4       3       1       2       2
6       0       3       2       4       0       9       4       4       9       3       0       1       0

Overall, I want to replace all the 0's with 1's in all even numbered fields. I think this could be done using a for loop, but I don't know how to just select the even numbered fields.

Thanks again!
# 7  
Old 01-27-2015
yes MadeinGermany, I have given code as user didn't specify that. User told he/she wants to replace all zeros, so only I have given gsub.

Hello Rabu,

Change the second solution to as follows, if you need only single 0 to be renamed then following may help.

Code:
awk '{if(length($2)==1){sub("0","1",$2)};if(length($4)==1){sub("0","1",$4)}} 1' OFS="\t"  Input_file

OR
If you want to change all only zeros in $2 and $4 which may have more than one length following may help.
Code:
awk '{if($2==0){gsub("0","1",$2)};if($4==0){gsub("0","1",$4)}} 1' OFS="\t"  Input_file


EDIT: Adding solution for Rabu's latest post for chaging every even field's zero to one, again considering that all fields have only one digit values.
Code:
awk '{for(i=1;i<=NF;i++){if(i%2==0){sub("0","1",$i)}}} 1' OFS="\t"  Input_file

Hope this helps you.

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-27-2015 at 03:35 AM.. Reason: Added a solution as per user's latest post
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Splitting a filed into multiple fields using awk

Hi, I have a tab delimited file as below: AWA Divi DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15 OT 01-MAY-15 31-OCT-15 CF 01-MAY-15 31-OCT-15 AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15 AD 01-MAY-15 31-OCT-15 The number of columns(fields) after 7th field is not fixed and... (3 Replies)
Discussion started by: Bobby_2000
3 Replies

3. Shell Programming and Scripting

awk multiple fields separators

Can you please help me with this .... Input File share "FTPTransfer" "/v31_fs01/root/FTP-Transfer" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "Test" "/v31_fs01/root/Test" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "ENR California" "/v31_fs01/root/ENR California"... (14 Replies)
Discussion started by: greycells
14 Replies

4. Shell Programming and Scripting

Find fields and replace using awk

Code: Using ksh Var1=`awk -F";" {print $1}' Input2.txt` cat Input1.txt | awk -F";" '{$3="Var1"}' > Output.txt (13 Replies)
Discussion started by: Roozo
13 Replies

5. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

6. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

7. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies

8. Shell Programming and Scripting

help with search and replace in multiple fields

I have a pipe delimited file with 27 fields. Each record has 26 fields. I need to search for the 25,26,27 fields and replace "," with nothing. How can I acheive this. Sed is more preferred. e.g data row o/p (5 Replies)
Discussion started by: dsravan
5 Replies

9. Shell Programming and Scripting

AWK multiple fields separators

I need to print the second field of a file, taking spaces, tab and = as field separators. ; for 16-bit app support MAPI=1 CMC=1 CMCDLLNAME32=mapi32.dll CMCDLLNAME=mapi.dll MAPIX=1 MAPIXVER=1.0.0.1 OLEMessaging=1 asf=MPEGVideo asx=MPEGVideo ivf=MPEGVideo m3u=MPEGVideo (2 Replies)
Discussion started by: PamPam
2 Replies

10. Shell Programming and Scripting

use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this count,1977,1978,1979 usa, , , blue japan, red, yellow,green india, , yellow,blue china, blue, yellow, green I want the output to be(replace everything, including empty data, with the most recent data): ... (1 Reply)
Discussion started by: grossgermany
1 Replies
Login or Register to Ask a Question