How To Arrange Record In A Special Order?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Arrange Record In A Special Order?
# 1  
Old 01-20-2014
How To Arrange Record In A Special Order?

Hi All,

I have following data to be arranged based on columnd 6:

Code:
0098442947
0098222107
0098442134
0098200179
0098441067
0098442744
0098443106
0098442746
0098321411
0098443111
0098456611
0098444570
0098456600
0098385750
0098200288
0098261708
0098443048
0098385362
0098385362
0098385362
0098385363
0098385363
0098385365
0098385365
0098385363


I want the above to be arranged in the following way:

Code:
0098321411
0098385362
0098385363
0098442744
0098385365
0098443106
0098442947
0098200288
0098200179
0098444570
0098443111
0098385362
0098385363
0098442134
0098385365
0098442746
0098222107
0098261708
0098456600
0098456611
0098385362
0098385363
0098441067
0098443048
0098385750

Is this possible to achieve by using awk? This field is actaully column 6 of the following data:

Code:
120|000000002|2PM||0|0098442940|0|0134515050|MOBILE 013|0||03|01|NL02||0|11|1|1|C|20130401175314|20130401175317|+0800|3|0|10|0||02|0|||M

Your input is highly appreciated

Thanks
Angsuman
# 2  
Old 01-20-2014
Hello,

Could you please try the following.

Code:
awk -F"|" '{print $6}' OFS="|" get_6th_column | grep -v '^$'

Output will be.

Code:
0098442940


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-20-2014 at 11:18 AM..
# 3  
Old 01-20-2014
You mean rearrange the multi-column inputfile in the order of a given 1 column orderfile?
If you have enough memory you can do
Code:
awk '
NR==FNR {arr[$6]=$0; next}  # file1: store the line in array addressed by column 6
{print arr[$1]}  # file2: print the stored line addressed by column 1
' inputfile orderfile

# 4  
Old 01-20-2014
I don't understand the logic of your sorting :

Shouldn't the value : 0098442134 and 0098442744 be switched in the expected result ?

A failed attempt but that may still give some ideas
Code:
$ awk '{n=$0%10;n=n?n:10;a[n]++;print $0,(n*(10^(a[n])))}' yourfile | sort -k 2n
0098321411 10
0098385362 20
0098385363 30
0098442134 40
0098385365 50
0098443106 60
0098442947 70
0098200288 80
0098200179 90
0098443111 100
0098444570 100
0098385362 200
0098385363 300
0098442744 400
0098385365 500
0098442746 600
0098222107 700
0098261708 800
0098456600 1000
0098456611 1000
0098385362 2000
0098385363 3000
0098441067 7000
0098443048 8000
0098385750 10000

Code:
$ awk '{l=$0%10;l=l?l:10;a[l]++;print $0,(l*(10^(a[l]-1)))}' yourfile | sort -k 2n
0098321411 1
0098385362 2
0098385363 3
0098442134 4
0098385365 5
0098443106 6
0098442947 7
0098200288 8
0098200179 9
0098443111 10
0098444570 10
0098385362 20
0098385363 30
0098442744 40
0098385365 50
0098442746 60
0098222107 70
0098261708 80
0098456600 100
0098456611 100
0098385362 200
0098385363 300
0098441067 700
0098443048 800
0098385750 1000

You can concatenate the line number to ensure unicity (here since your file does not contain more than 999 lines, the line number can be written on 3 digit)

Code:
$ awk '{l=$0%10;l=l?l:10;a[l]++;printf "%s %s%03g\n",$0,(l*(10^(a[l]-1))),NR}' yourfile | sort -k 2n
0098321411 1009
0098385362 2018
0098385363 3021
0098442134 4003
0098385365 5023
0098443106 6007
0098442947 7001
0098200288 8015
0098200179 9004
0098443111 10010
0098444570 10012
0098385362 20019
0098385363 30022
0098442744 40006
0098385365 50024
0098442746 60008
0098222107 70002
0098261708 80016
0098456611 100011
0098456600 100013
0098385362 200020
0098385363 300025
0098441067 700005
0098443048 800017
0098385750 1000014

Just feel free to find shorter or better way and/or formula to achieve this and let us know

Last edited by ctsgnb; 01-20-2014 at 02:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help to delete special characters exists only at the end of the each record in UNIX file?

Hi, I have a file in unix with 15 columns.It consists special characters(#,$,^M,@,*,% etc)at the end of the each record.I want to remove these special characters.I used the following: Sed -e 's/ /g;s/ */ /g' . But It is removing special characters exists everywhere in the file(begining,middle... (24 Replies)
Discussion started by: rakeshp
24 Replies

2. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

4. Shell Programming and Scripting

Arrange values in ascending order

HI I have a file # vi assc values order fin 100 34 45 200 12 64 120 10 23 Here I need to check whether the values of second column"order" is arranged ascendingly Note: Always order column will be arranged either in ascending or descending order How to make it?... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

5. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. Programming

generating pair of numbers in special order

I need some help in generating pair of numbers in orders using FORTRAN code. The order is like following. loop_1: 1,2 2,3 3,4 4,5 5,6 6,7 7,8 ..... until <= 2000 loop_2: 1,3 3,5, 5,7 7,9 9,11 11,13 ........until <= 2000 loop_3: 1,4, 4,7 7,10 10,13 13,17 ..... until... (3 Replies)
Discussion started by: vjramana
3 Replies

8. UNIX for Advanced & Expert Users

How to remove duplicate lines of a record without changing the order

Hi all, I have to remove duplicate lines in a file without chainging the order.for eg if i have a record pqr def abc lmn pqr abc mkh hgf the output should be pqr def abc lmn mkh hgf (7 Replies)
Discussion started by: abhi.roy03
7 Replies

9. UNIX for Dummies Questions & Answers

change order of fields in header record

Hello, after 9 months of archiving 1000 files, now, i need to change the order of fields in the header record. some very large, space padded files. HEADERCAS05212008D0210DOMEST01(spacepadded to record length 210) must now be 05212008HEADERCASD0210DOMEST01(spacepadded to record length 210) ... (1 Reply)
Discussion started by: JohnMario
1 Replies

10. UNIX for Dummies Questions & Answers

reconstructing a record in a diffrent order

Can sed be used to take a existing record and reverse the order of defined character placement if there is no delimeters? existing record: 0123456789CO expected result: 9876543210CO if there were delimeters I could define the delimeter and each placement would have an id which I... (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question