Sed/awk to find negative numbers and replace with 1?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed/awk to find negative numbers and replace with 1?
# 1  
Old 04-10-2014
Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't make it work.

Input:
Code:
3.894672	4221	4232
79.974159	-63	76
26.139051	1786	1798
30.866007	5045	5055
42.381535	-199	205
17.103092	-953	961
29.135157	1365	1384
29.135157	1643	1658

Desired Output:
Code:
3.894672	4221	4232
79.974159	1	76
26.139051	1786	1798
30.866007	5045	5055
42.381535	1	205
17.103092	1	961
29.135157	1365	1384
29.135157	1643	1658

Thanks!
# 2  
Old 04-10-2014
Code:
awk '{$2=($2<0)?1:$2}1' myFile
OR
sed 's/-[0-9][0-9]*/1/'  myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-10-2014
This works like a charm, except that it takes a tab-delimited file and returns a space-delimited file. My fault for not mentioning it in the original post.
# 4  
Old 04-10-2014
OFS variable you have to use like below
Code:
awk '{$2=($2<0)?1:$2}1' OFS='\t' myfile

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 04-10-2014
Quote:
Originally Posted by Twinklefingers
This works like a charm, except that it takes a tab-delimited file and returns a space-delimited file. My fault for not mentioning it in the original post.
Code:
awk '{$2=($2<0)?1:$2}1' OFS=='\t'  myFile

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 04-10-2014
Variations of vgersh99's solution which don't rebuild $0 when $2 is non-negative:
Code:
awk '$2>=0 || ($2=1)' OFS='\t' myFile

or
Code:
awk '$2<0 {$2=1} 1' OFS='\t' myFile

Regards,
Alister

Last edited by alister; 04-10-2014 at 04:52 PM..
These 2 Users Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed to replace / between the two numbers

i Have a file as following view pz19a0c0/1000T_J_3MoDw9DSLh1ZsCubdua-LKOQmbtiVgkIsiMbSiwF467?sessionId=15451401994597121249 view pz19a0c0/100086X67pR0MwzWnhhSO6sAEoxeFMyhh-IIbUCCdxicaQM4FC9?sessionId=154514019945971212494898 view/cart ... (5 Replies)
Discussion started by: Raghuram717
5 Replies

2. Shell Programming and Scripting

sed and awk -Find and Replace

All, I have thousands of lines in a file with following format DATA=_ONE_XXX_YYY_CCC_HHHG_ DATA1=_GGG_JJJJ_HHH_UUU_JJJJ_HHHH_LLL_ DATA3=_MMM_GG_NN_QQQQ_FFF_III_ I want to replace _ with . by ignoring the first (=_) and last (_) So that out put should looks like... (4 Replies)
Discussion started by: baluchen
4 Replies

3. Shell Programming and Scripting

In a row, replace negative sign and find minimum value among four columns

Hi Friends, I have an input file like this chr1 100 200 1 2 3 4 chr1 150 200 4 5 6 7 chr2 300 400 9 6 7 1 chr2 300 410 -10 21 -11 13 chr3 700 900 -21 -22 130 165 Now, my output file is chr1 100 200 1 chr1 150 200 4 chr2 300 400 1 chr2 300 410 10 chr3 700 900 21 Remove... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

5. Shell Programming and Scripting

using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do. I have a text file like this.... 1! SRCNAM = 00001 ! 1! X = 50.0000, 0.0000,... (10 Replies)
Discussion started by: mercury.int
10 Replies

6. Shell Programming and Scripting

sed&awk: replace lines with counting numbers

Dear board, (I am trying to post this the 3rd time, seems there's some conflicts with my firefox with this forum, now use IE) ------ yes, I have searched the forum, but seems my ? is too complicated. ------------origianl file --------------- \storage\qweq\ertert\ertert\3452\&234\test.rec... (4 Replies)
Discussion started by: oUo
4 Replies

7. Shell Programming and Scripting

find all numbers > x and replace with y within a file

How would I do this? How could i use <> symbols for numbers in the find/replace code below? perl -pi -e 's/test/tst/' OR is there a better way? 100 5000 2 432 4 2 33 4 5 6 65 300 301 needs to be: 100 300 2 300 4 2 33 4 5 6 65 300 300 also it might not always need spaces... i... (12 Replies)
Discussion started by: herot
12 Replies

8. Shell Programming and Scripting

Find and replace with variable using sed or awk

hi, i have file say email.temp looks like Bell_BB 17 Bell_MONTHLY 888 SOLO_UNBEATABLE 721 and another file r3 Bell BB,Bell_BB Bell,Bell_MONTHLY SOLO,SOLO_UNBEATABLE i want email.temp files $1 say Bell_BB should be replaced by r3 Bell BB and Bell_MONTHLY by... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

9. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

10. Shell Programming and Scripting

[sed/awk] find info and replace

Hi :) I have some problems with "FOR"... I have a text file in this format: name1 www.link1/random_number name2 www.link2/random_number name3 www.link3/random_number ... (Names and info changes) Now, I need: (4 Replies)
Discussion started by: aspire
4 Replies
Login or Register to Ask a Question