Absolute minimum value of a number using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Absolute minimum value of a number using awk
# 1  
Old 08-22-2013
Linux Absolute minimum value of a number using awk

Hi,

I have got a file in the following format:
Code:
4300 23695
4305 03591
4400 125368

I need to sort this file to find the minimum absolute value of the numbers starting from 6th pos, so the expected output is:-
Code:
4300 23569
4305 01359
4400 123568

I tried to cut out the file from 6th position and sort the file using asort function....but in vain.....

need some help....SmilieSmilie

Last edited by Franklin52; 08-22-2013 at 08:28 AM.. Reason: Please use code tags
# 2  
Old 08-22-2013
Quote:
Originally Posted by roy121
I tried to cut out the file from 6th position and sort the file using asort function....but in vain.....

need some help....SmilieSmilie
Please show us your attempted solution.
# 3  
Old 08-22-2013
i used :
Code:
awk '{  n=split($0,arr)  asort(arr)  for(i=0;i<=n;i++)     print arr[i]}' file


but i am getting syntax error...

Last edited by Franklin52; 08-22-2013 at 10:56 AM.. Reason: Please use code tags
# 4  
Old 08-22-2013
try:
Code:
awk '{ n=split($2,arr) ; asort(arr) ; printf $1 " " ; for(i=0;i<=n;i++) printf arr[i]; print ""}' file

# 5  
Old 08-22-2013
Perl version:

Code:
cat file

4300 23695
4305 03591
4400 125368

perl -lane '@a = split(//, $F[1]);@sorted = sort {$a <=> $b} @a;printf("%s ", $F[0]);print @sorted' file

4300 23569
4305 01359
4400 123568

# 6  
Old 08-22-2013
Although it should work, the following is an inefficient approach. I post it for the enjoyment.
Code:
while read a b; do
    echo $a $(echo $b | fold -w1 | sort | paste -sd '')
done

Assumptions are that the file consists of nothing but digits and whitespace.

Regards,
Alister
# 7  
Old 08-22-2013
You need to split $2 with "" (null separator). And the array index starts with 1.
Code:
awk '{n=split($2,arr,""); asort(arr); printf "%s ",$1; for(i=1;i<=n;i++) printf "%s",arr[i]; print ""}' file

Another output formatting:
Code:
awk '{n=split($2,arr,""); asort(arr); s=""; for(i=1;i<=n;i++) s=s""sprintf("%s",arr[i]); print $1,s}' file


Last edited by MadeInGermany; 08-22-2013 at 02:45 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Extract minimum/maximum using awk

From the below table I want to print highest value and lowest value using awk script. aaa 55 66 96 77 ggg 22 96 77 23 ddd 74 58 18 3 kkk 45 89 47 92 zzz 34 58 89 92 Thanks, Green edit by bakunin: it sure is not news to you that you should use CODE-tags, no? And that we do not want such... (3 Replies)
Discussion started by: gwgreen1
3 Replies

4. Shell Programming and Scripting

Print minimum and maximum values using awk

Can I print the minimum and maximum values of values in first 4 columns ? input 3038669 3038743 3037800 3038400 m101c 3218627 3218709 3217600 3219800 m290 ............. output 3037800 3038743 m101c 3217600 3219800 m290 (2 Replies)
Discussion started by: quincyjones
2 Replies

5. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Using awk to obtain minimum of each column (ignoring zeros)

Hi, I have a wide and long dataset which looks as follows: 0 3 4 2 3 0 2 2 ... 3 2 4 0 2 2 2 3 ... 0 3 4 2 0 4 4 4 ... 3 0 4 2 2 4 2 4 ... .... I would like to obtain the minimum of each column (ignoring zero values) so the output would look like: 3 2 4 2 2 2 2 2 I have the... (3 Replies)
Discussion started by: kasan0
3 Replies

7. Shell Programming and Scripting

using awk to print absolute value

Hi, I have a file contaning some variables with negative values, but i just want to print the positive value awk -F"|" '$2<0 { print $1,$2 }' tom.unl > tee.unl i want the $2 = absolute value eg -200 should print 200 How can i do this. (2 Replies)
Discussion started by: dealerso
2 Replies

8. Linux

What is the minimum number of partitions you need to install Linux?

I think its 3. Just to know if I am correct. / /boot swap :confused: (2 Replies)
Discussion started by: nitin09
2 Replies

9. Shell Programming and Scripting

minimum number of unique key

input a 1 a 2 a -1 b 1 b 2 b 3 output a -1 b 1 Thanx ---------- Post updated at 09:42 PM ---------- Previous update was at 09:10 PM ---------- Ok I managed it (7 Replies)
Discussion started by: repinementer
7 Replies

10. Shell Programming and Scripting

Find the minimum number

Input 10 8 20 8 10 9 20 9 10 12 20 19 10 10 20 40 Output1 10 8 2 20 8 12 10 9 1 20 9 11 10 12 -2 20 19 1 10 10 0 20 40 -20 Output2 10 9 ... (0 Replies)
Discussion started by: repinementer
0 Replies
Login or Register to Ask a Question