0

I have a data like this

JW_ID   ECK number  B number    T Number    M K12   Conc(%) Yield(uM)   Yield(ug/ml)
JW0002  ECK0003       b0003        thrB       thrB    32        2.3         78
JW0003  ECK0004       b0004        thrC       thrC    18        1.7         78
JW0004  ECK0005       b0005        yaaX       yaaX    78        1.2         14
JW0005  ECK0006       b0006        yaaA        yaaA   7         2.4         71
JW0007  ECK0008       b0008        talB        talB   85        2.7         94

I am trying to remove two columns and B Number and M K12 then sort the file by ascending based on Conc(%) Yield(uM) Yield(ug/ml)

I run it as

bash myprogram.sh filename.txt

I have tried to make a bash but no success, I would appreciate any help

#!/bin/bash
filename=$1
while read -r line; do
# reading each line
awk '{$3=$5=""; print $0}' filename
sort -k6,1 -k7,2 -k8,3 filename
echo $line
done < $filename

the expected output is like this

JW_ID   ECK number  T Number    Conc(%) Yield(uM)   Yield(ug/ml)
JW0005  ECK0006.        yaaA        7        2.4    71
JW0003  ECK0004.        thrC        18       1.7    78
JW0002  ECK0003         thrB        32       2.3    78
JW0004  ECK0005.        yaaX        78       1.2    14
JW0007  ECK0008         talB        85       2.7    94
4
  • your file is Tab separated columns? – αғsнιη Feb 19 at 21:41
  • @αғsнιη that is right tab delimited text – nik Feb 19 at 21:43
  • and expected output would be like what? is this what you want <infile cut -f1,2,4,6- |sort -t $'\t' -k4,6n? if yes, I will answer it – αғsнιη Feb 19 at 21:58
  • @αғsнιη I just added an expected output but could you make it more general? imagine if I have 10 columns to sort. by and after those columns I still have other columns , you know what I mean? – nik Feb 19 at 23:49
0

This awk should give the desired results:

$ awk -F '\t' 'BEGIN{OFS="\t"} { print $1,$2,$4,$6,$7,$8 }' data | sort -nk 4,6
JW_ID   ECK number      T Number        Conc(%) Yield(uM)       Yield(ug/ml)
JW0005  ECK0006 yaaA    7       2.4     71
JW0003  ECK0004 thrC    18      1.7     78
JW0002  ECK0003 thrB    32      2.3     78
JW0004  ECK0005 yaaX    78      1.2     14
JW0007  ECK0008 talB    85      2.7     94     

If your field count is much higher, here's a version with a range function:

usage: rng([begin],[end])

$ awk -F '\t' '
function rng(b,e,  i,d){ d=$b;
  for(i=b+1;i<e;i++){ d=d""OFS""$i }
  d=d""OFS""$e;
  return d
}
BEGIN{OFS="\t"}
{ print rng(1,2),$4,rng(6,8) }' data | sort -nk 4,6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.