Sort two columns with alphanumeric values horizontally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort two columns with alphanumeric values horizontally
# 1  
Old 07-25-2017
Sort two columns with alphanumeric values horizontally

Hi, I have a file like
Code:
aa	bb	dmns|860	dmns|756	ee	ff
aa	bb	dmns|310	dmns|260	ee	ff
aa	bb	dmns|110	dmns|77	ee	ff
aa	bb	dmns|756	dmns|860	ee	ff
aa	bb	dmns|110	dmns|77	ee	ff
aa	bb	dmns|233	dmns|79	ee	ff
aa	bb	dmns|79	dmns|233	ee	ff

I want to sort the values in column3 and column4 horizontally. Rest column will be fixed after sorting.
Here is the desired output
Code:
aa	bb	dmns|756	dmns|860	ee	ff
aa	bb	dmns|260	dmns|310	ee	ff
aa	bb	dmns|77	dmns|110	ee	ff
aa	bb	dmns|756	dmns|860	ee	ff
aa	bb	dmns|77	dmns|110	ee	ff
aa	bb	dmns|79	dmns|233	ee	ff
aa	bb	dmns|79	dmns|233	ee	ff

I tried these with no success
Code:
awk -F, '{print $3, $4}' file | sort -V | sed 's/^[0-9][0-9]* //'

awk -F, '{print $3, $4}' file | sort -n -k1.6 | sed 's/^[0-9][0-9]* //'

awk '{print $3, $4}' test_filter_by_score.txt | perl -ane '$,=" "; print sort @F; print "\n";'

# 2  
Old 07-26-2017
Hello sammy777888,

Considering that your Input_file is same as shown sample Input_file, if that is the case then following may help you in same.
Code:
awk -F"\t" '{sub(/ /,"",$3);split($3,a,"|");q=a[2];v=a[3];sub(/[0-9]+/,"",q);sub(/[0-9]+/,"",v);if(a[2]+0>a[3]+0){tmp=a[2]+0;a[2]=a[3]+0;a[3]=tmp+0};$3=sprintf("%s|%d%s%s|%d%s%s",a[1],a[2]+0,length(a[2]+0)>2?"":" ",q,a[3]+0,length(a[3]+0)>2?"":" ",v)}1' OFS="\t"  Input_file

Output will be as follows.
Code:
aa      bb      dmns|756dmns|860ee      ff
aa      bb      dmns|260dmns|310ee      ff
aa      bb      dmns|77 dmns|110ee      ff
aa      bb      dmns|756dmns|860ee      ff
aa      bb      dmns|77 dmns|110ee      ff
aa      bb      dmns|79 dmns|233ee      ff
aa      bb      dmns|79 dmns|233ee      ff

EDIT: Adding a non-one liner form of solution too now.
Code:
awk -F"\t" '{
    sub(/ /,"",$3);
    split($3,a,"|");
    q=a[2];
    v=a[3];
    sub(/[0-9]+/,"",q);
    sub(/[0-9]+/,"",v);
    if(a[2]+0>a[3]+0){
        tmp=a[2]+0;
        a[2]=a[3]+0;
        a[3]=tmp+0
};
    $3=sprintf("%s|%d%s%s|%d%s%s",a[1],a[2]+0,length(a[2]+0)>2?"":" ",q,a[3]+0,length(a[3]+0)>2?"":" ",v)
}
1
' OFS="\t"   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-26-2017 at 12:43 AM.. Reason: Adding a non-one liner form of solution too now.
# 3  
Old 07-26-2017
Try also
Code:
awk '
        {T = " " $3 "  " $4
         gsub (/ [^|]*\|/, _, T)
         split (T, TN)
         if (TN[1] > TN[2])     {T  = $3
                                 $3 = $4
                                 $4 = T
                                }
        }
1
' OFS="\t" file
aa	bb	dmns|756	dmns|860	ee	ff
aa	bb	dmns|260	dmns|310	ee	ff
aa	bb	dmns|77	dmns|110	ee	ff
aa	bb	dmns|860	dmns|756	ee	ff
aa	bb	dmns|77	dmns|110	ee	ff
aa	bb	dmns|79	dmns|233	ee	ff
aa	bb	dmns|233	dmns|79	ee	ff

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Want to sort a file which contains alphanumeric strings

I want to sort a file which contains alphanumeric string. bash-3.00$ cat abc mtng1so mtng2so mtng11so mtng9so mtng23so mtng7so hstg2so hstg9so hstg1so hstg11so hstg13so bash-3.00$ Want output like this, using one liner. hstg1so (1 Reply)
Discussion started by: Raza Ali
1 Replies

2. Shell Programming and Scripting

Trouble with alphanumeric Sort

I have a lot of data that need to be sorted alphanumerically. I began using sort -du and it solved almost all my problems. However, when I encountered files with data like this it began to fail: /vol/close_eng_ice_0888 /vol/open_eng_ice_0890 /vol/open_eng_ice_08923 /vol/open_eng_ice_0893... (6 Replies)
Discussion started by: newbie2010
6 Replies

3. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

4. Shell Programming and Scripting

Sort each row (horizontally) in AWK or any

Hello, How to sort each row in a document with numerical values and with more than one row. Example Input data (file1.txt): 4 6 8 1 7 2 12 9 6 10 6 1 14 5 7 and I want the the output to look like this(file2.txt): 1 4 6 7 8 2 6 9 10 12 1 5 6 7 14 I've tried sort -n file1.txt >... (12 Replies)
Discussion started by: joseamck
12 Replies

5. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Shell Programming and Scripting

Get values from different columns from file2 when match values of file1

Hi everyone, I have file1 and file2 comma separated both. file1 is: Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9,Header10 Code7,,,,,,,,, Code5,,,,,,,,, Code3,,,,,,,,, Code9,,,,,,,,, Code2,,,,,,,,,file2... (17 Replies)
Discussion started by: cgkmal
17 Replies

7. Shell Programming and Scripting

ksh to compare alphanumeric values from 2 files

Hi there, I want to compare 2nd column which are alphanumeric values from each of the 2 files i.e.,lspv_pre.out and lspv_post.out , if found echo some message. lspv_pre.out hdisk0 00c39eaa451144dd rootvg active hdisk1 00c39eaa45223322 ... (3 Replies)
Discussion started by: mbak
3 Replies

8. UNIX for Dummies Questions & Answers

Sort by columns

Hello, I have a text file that looks like this and I need a bash script to: 12:48:32 PM 002* OUT 000418 01:10:34 PM 002* ONL 000418 01:49:17 PM 001* OUT 000364 01:52:09 PM 001* ONL 000364 ... The fields are: 12-hour format time, some number, state (online, offline) and another... (2 Replies)
Discussion started by: Ravendark
2 Replies

9. Shell Programming and Scripting

sort command - alphanumeric

I have a file I'm trying to sort such as fred1 fred2 fred10 fred11 ... when I sort I get fred1 fred10 fred11 fred2 ... using sort can any give me the syntax to sort this is dict order e.g., (4 Replies)
Discussion started by: gefa
4 Replies

10. UNIX for Dummies Questions & Answers

Sort by Columns

Hello, I am new in UNIX I am looking for a instrction to sort a file by columns 6,25 and 41 this is what I tried but not getting the correct result: sort -t= -k1.6,1.25,1.41 to_sort.txt > sorted.txt I used -t= just to get the whole line as one field. INVS80993596SUM994338602XX... (1 Reply)
Discussion started by: murbina
1 Replies
Login or Register to Ask a Question