Require original field position after sorting the values in a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Require original field position after sorting the values in a record
# 1  
Old 10-07-2013
Require original field position after sorting the values in a record

Dear Team,

Can any body help me out to get the filed position of the records post sorting using AWK programming.

Thanks in advance

Example Input:
Code:
StudentID   col-1   col-2   col-3   col-4       
1234   14   10   12   13       
1235   10   11   12   13       
1236   13   12   11   10       
2345   20   30   10   50       
2346   20   21   24   23       
2347   14   14   13   12       
3312   10   11   10   12       
3313   15   16   17   15

Example Output:
Code:
1234   2   3   4   1       
1235   1   2   3   4       
1236   4   3   2   1       
2345   3   1   2   4       
2346   1   2   4   3       
2347   4   3   1   2       
3312   1   3   2   4       
3313   1   4   2   3


Last edited by Franklin52; 10-07-2013 at 07:53 AM.. Reason: Please use code tags
# 2  
Old 10-07-2013
Please use code tag and explain clearly
# 3  
Old 10-10-2013
I have an array in which I would require original position of the value post sorting.

Below is simple script which I written using asort function to get the values sorted, but I need output as initial array original filed position after sorting.


-bash-3.2$ awk 'BEGIN {a[1]=1234; a[2]=14; a[3]=10; a[4]=12; a[5]=13; n=asort(a,dest); for (i=1;i<=n;i++) { printf("%s,", dest[i])}}'
10,12,13,14,1234,-bash-3.2$
-bash-3.2$

Excepted :
3,4,5,2,1
# 4  
Old 10-10-2013
Quote:
Originally Posted by Srinivasa Reddy
I have an array in which I would require original position of the value post sorting.

Below is simple script which I written using asort function to get the values sorted, but I need output as initial array original filed position after sorting.


-bash-3.2$ awk 'BEGIN {a[1]=1234; a[2]=14; a[3]=10; a[4]=12; a[5]=13; n=asort(a,dest); for (i=1;i<=n;i++) { printf("%s,", dest[i])}}'
10,12,13,14,1234,-bash-3.2$
-bash-3.2$

Excepted :
3,4,5,2,1


Try this

Code:
 $ cat test.sh
#!/bin/bash

echo | \
awk '
    function keys(arr,key){
                           for(z in arr){if(arr[z]==key)return z}
                           return "Not Found"
                          }
BEGIN {
           OFS="\t"
           a[1]=1234
           a[2]=14 
           a[3]=10 
           a[4]=12 
           a[5]=13 

    n=asort(a,dest)

    for(i=1;i<=n;++i)
                    printf "Value ----->" OFS dest[i] OFS "Original Index---->"OFS keys(a,dest[i]) RS
      }'

resulting
Code:
$ sh test.sh 
Value ----->    10    Original Index---->    3
Value ----->    12    Original Index---->    4
Value ----->    13    Original Index---->    5
Value ----->    14    Original Index---->    2
Value ----->    1234  Original Index---->    1


Last edited by Akshay Hegde; 10-10-2013 at 02:21 AM.. Reason: formatting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shift record from one position to another

Hi All, I have a file and it is a fixed length file. I want to move the values from 42,6 ( where 6 is length) to the 36th position Original file: 00000100000100000100000100000100001 000870 ... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output HDR|F1|F2||F4|F5|F6|F7 I am able to append the string to the end of the record, but not in between the... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

6. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

7. UNIX for Dummies Questions & Answers

how to copy files and record original file location?

:EDIT: I think my post name should have been labeled: how to copy files and record original file location. not "retain". Hello, this is my first post! I searched the forums a lot before posting, but was unable to answer my question. Here's my problem: There are several hundred text files... (4 Replies)
Discussion started by: willie8605
4 Replies

8. Shell Programming and Scripting

Search and Replace by record position

Hi All, I have a file that I would like to search for data and replace other data by record position number: Example search.. search for "CLARK KENT" and replace Amt data "000025" with "000155"??? I'm able to search and replace unique data but, came to a stump when wanting to replace data... (11 Replies)
Discussion started by: macastor
11 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

Add 'ENDEND' on end of each record at position is 14-20

I have file format like below and I'm trying to modify this file. I need to add 'ENDEND' end of each record. 01 ASH01 1CTCTL EDPPOO STAND 01 ASH08 0020 A1TH 101 01 ASH09 0022 A1TH 102 01 ASH09 0022 A1TH 103 01 ASH02 2CTCTL ... (5 Replies)
Discussion started by: naveenkcl
5 Replies
Login or Register to Ask a Question