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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In a row, replace negative sign and find minimum value among four columns
# 1  
Old 02-19-2013
In a row, replace negative sign and find minimum value among four columns

Hi Friends,

I have an input file like this

Code:
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

Code:
chr1 100 200 1
chr1 150 200 4
chr2 300 400 1
chr2 300 410 10
chr3 700 900 21

Remove negative signs from columns 4,5,6 and7 and then find the minimum value among these 4 columns for a row, and print the minimum value.

Thanks in advance.
# 2  
Old 02-19-2013
Try:
Code:
awk '{for (i=4;i<=NF;i++) {if ($i<0) $i*=-1; $4=($i<$4)?$i:$4} print $1,$2,$3,$4}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-19-2013
Code:
awk 'min="x" {for (i=4; i<=NF; i++) min=(min>$i)?$i:min} {print $1,$2,$3,sqrt(min*min)}' infile

This User Gave Thanks to Jotne For This Post:
# 4  
Old 02-19-2013
Always 7 fields? bash has trinary operator ?:
Code:
while read x y z a b c d
do
 (( a = a < 0 ? 0 - a : a ))
 (( b = b < 0 ? 0 - b : b ))
 (( c = c < 0 ? 0 - c : c ))
 (( d = d < 0 ? 0 - d : d ))
 (( m = a > b ? b : a ))
 (( m = m > c ? c : m ))
 (( m = m > d ? d : m ))
 echo $x $y $z $m
done < in_file

No multiply or sqrt. Could nest the last three. "tr -d '-' |" would remove the negatives in the string space.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 02-19-2013
try also (based on bartus11 solution):
Code:
awk '{for (i=4;i<=NF;i++) {$i*=$i>0?1:-1; $4=$i<$4?$i:$4}} NF=4' infile

# 6  
Old 02-20-2013
Shouldn't you start with 5, as 4 is neve less than 4?

0-$i save a multiply.

(Easier to criticize that to create! Slowly learning awk by osmosis!)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

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

2. Shell Programming and Scripting

How to find a minimum value of a row?

input 1 2 3 4 5 2 8 2 1 1 1 4 2 1 5 4 4 4 2 1 3 2 2 6 7 4 5 4 5 5 5 4 3 3 5 I woud like to print a min of each row such that my output would look like 1 1 1 2 3 (5 Replies)
Discussion started by: johnkim0806
5 Replies

3. Shell Programming and Scripting

minimum and maximum from columns

Hi Friends, my input file is this way chr1 100 120 abc chr1 100 121 def chr1 100 122 ghi chr2 240 263 kil chr2 240 276 ghj chr2 255 290 hjh my output chr1 100 122 abc chr2 240 276 kil chr2 255 290 hjh Basically, I want to match on first and second column and then print the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

5. Shell Programming and Scripting

Help to move leading negative sign to trailing position

Hi All, I am having a file which contains negative numbers, wht i am doing is re-formattting the file(moving few columns and add few hard codings between), while reformatting i would want the negative numbers to have the sign as trailing rather than leading. Existing -2400.00 34 0.00... (11 Replies)
Discussion started by: selvankj
11 Replies

6. UNIX for Dummies Questions & Answers

Extract minimum values among 3 columns

Hi. I would like to ask for helps on extracting a minimum values among three columns using gawk in tab separator. input file: as1 10 20 30 as2 22 21 23 as3 300 391 567 as4 19 20 15 Output file: as1 10 as2 21 as3 300 as4 15 I am extremely appreciate your helps and comments.... (2 Replies)
Discussion started by: Amanda Low
2 Replies

7. Programming

Select several minimum values from row (MySQL)

Hello there. I've got the query like that SELECT count(tour_id) AS cnt FROM orders JOIN tours ON orders.tour_id=tours.id GROUP BY tour_id The result Is cnt 1 4 2 1 1 Now i have to select all records with minimum values in field "cnt" MySQL function min() returns only one.... (2 Replies)
Discussion started by: Trump
2 Replies

8. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies

9. Shell Programming and Scripting

Count minimum columns in file

Hi All, Consider the file with following lines: 1,2,3,4 1,2,3, 5,6,7,7,8,9 1 I need to get the count of minimum columns per line. i.e. in above case, it should come out to be 1 since the last line only has 1 column. I tried following code: minCount = 0 wordCountPerLine = 0... (12 Replies)
Discussion started by: sh_kk
12 Replies

10. Shell Programming and Scripting

Finding Minimum value per Row range of data

Here is an example of a file I am working with: C 4704 CB 1318 ASP 115 BGRF 1 weak 0.0% 4.33 C 4720 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.71 O 4723 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.48 O 4723 CG 1321 ASP 115 BGRF 1 weak 0.0% 4.34... (3 Replies)
Discussion started by: userix
3 Replies
Login or Register to Ask a Question