I'm trying to rearrange from specific string into respective column.
etc:
126N (will be sorted into "Normal" column)
Value 1 (the integer will be concatenated with 126)
Resulting :
N=Normal
126 # 1
Here is the input
(N=Normal, W=Weak)
Value 1
126N,
Value 3
18N,
Value 4
559N, 562N, 564N,
Value 6
553W, 565A, 553N,
Value 5
490W,
Value 9
564N,
And the output should be
W=Weak
490 # 5
553 # 6
A=Absolute
565 # 6
N=Normal
126 # 1
18 # 3
559 # 4
562 # 4
564 # 4
553 # 6
564 # 9
Let me know your thought on this.
I've tried this script, I'm still figuring out to concatenating the value
cat input.txt | sed '/^\s*$/d' | awk 'BEGIN{RS=","};match($0,/N/){print $3"c"$2}' | sed ':a;N;$!ba;s/\n/;/g' | sed 's/W//g;s/N//g;s/S//g'
And some of it, are missing
This should give you what you want using gnu awk
IT will work with any number of letters, not just A N W
awk -F, '
!/Value/ {
for (i=1;i<NF;i++) {
hd=substr($i,length($i),1);
arr[hd][++cnt[hd]]=($i+0" # "f)}
}
{split($0,b," ");f=b[2];}
END {
for (i in arr) { print "\n"i"\n---";
for (j in arr[i]) {
print arr[i][j]}}
}' file
A
---
565 # 6
N
---
562 # 4
564 # 4
553 # 6
564 # 9
126 # 1
18 # 3
559 # 4
W
---
553 # 6
490 # 5
Another alternative in awk would be:
awk -F',| ' '
$1 == "Value" {value = $2; next}
{ for (i=1; i<=NF; i++) {
if ($i~"N$")
N[substr($i, 1, length($i) - 1)] = value
if ($i~"W$")
W[substr($i, 1, length($i) - 1)] = value
}
}
END {
print "W=Weak"
for (i in W)
print i, "#", W[i]
print "\nN=Normal"
for (i in N)
print i, "#", N[i]
}
' file
(note: this relies on knowing the wanted headers are W=Weak and N=Normal. If would take a few additional expression if the headers are subject to change.)
Output
$ awk -F',| ' '
> $1 == "Value" {value = $2; next}
> { for (i=1; i<=NF; i++) {
> if ($i~"N$")
> N[substr($i, 1, length($i) - 1)] = value
> if ($i~"W$")
> W[substr($i, 1, length($i) - 1)] = value
> }
> }
> END {
> print "W=Weak"
> for (i in W)
> print i, "#", W[i]
> print "\nN=Normal"
> for (i in N)
> print i, "#", N[i]
> }
> ' file
W=Weak
490 # 5
N=Normal
18 # 3
126 # 1
559 # 4
562 # 4
564 # 9
$ cat tst.awk
NR%2 { val = $NF; next }
{
for (i=1; i<=NF; i++) {
num = $i+0
abbr = $i
gsub(/[^[:alpha:]]/,"",abbr)
list[abbr] = list[abbr] num " # " val ORS
}
}
END {
n = split("Weak Absolute Normal",types)
for (i=1; i<=n; i++) {
name = types[i]
abbr = substr(name,1,1)
print abbr "=" name ORS list[abbr]
}
}
.
$ awk -f tst.awk file
W=Weak
553 # 6
490 # 5
A=Absolute
565 # 6
N=Normal
126 # 1
18 # 3
559 # 4
562 # 4
564 # 4
553 # 6
564 # 9
Related
This new question is a followup from a recent question : Compare two numerical ranges in two distincts files with awk. The proposed solution that perfectly worked was not practical for downstream analysis (misconception of my question, not on the solution that worked).
I have a file1 with 3 columns. Columns 2 and 3 define a numerical range. Data are sorted from the smaller to the bigger value in column 2. Numerical ranges never overlap.
file1
S 24 96
S 126 352
S 385 465
S 548 600
S 621 707
S 724 736
I have a second file2 (test) structured similarly.
file2
S 27 93
S 123 348
S 542 584
S 726 740
S 1014 2540
S 12652 12987
Desired output: Print ALL lines from file1 and next to them, lines of file2 for which numerical ranges overlap (including partially) the ones of file1. If no ranges from file2 overlap to a range of file1, print zero next to the range of file 1.
S 24 96 S 27 93 * 27-93 overlaps with 24-96
S 126 352 S 123 355 * 123-355 overlaps with 126-352
S 385 465 0 * nothing in file2 overlaps with this range
S 548 600 S 542 584 * 542-584 overlaps with 548-600
S 621 707 0 * nothing in file2 overlaps with this range
S 724 736 S 726 740 * 726-740 overlaps with 724-736
Based on the answer of the previous question from #EdMorton I modified the print command of the tst.awk script to add these new features. In addition I also changed the order file1/file2 to file2/file1 to have all the lines from file1 printed (whether or not there is a match in the second file)
'NR == FNR {
begs2ends[$2] = $3
next
}
{
for (beg in begs2ends) {
end = begs2ends[beg] + 0
beg += 0
if ( ( ($2 >= beg) && ($2 <= end) ) ||
( ($3 >= beg) && ($3 <= end) ) ||
( ($2 <= beg) && ($3 >= end) ) ) {
print $0,"\t",$1,"\t",beg,"\t",end
else
print $0,"\t","0"
next
}
}
}
Note: $1 is identical in file1 and file2. This is why I used print ... $1 to make it appear. No idea how to print it from file2 and not file1 (if I understand correctly this $1 refers to file1.
And I launch the analysis with awk -f tst.awk file2 file1
The script is not accepting the else argument and I dont understand why? I assuming that it is linked to the looping but I tried several changes without any success.
Thanks if you can help me with this.
Assumptions:
a range from file1 can only overlap with one range from file2
The current code is almost correct, just need some work with the placement of the braces (using some consistent indentation helps):
awk '
BEGIN { OFS="\t" } # output field delimiter is "\t"
NR == FNR { begs2ends[$2] = $3; next }
{
# $1=$1 # uncomment to have current line ($0) reformatted with "\t" delimiters during print
for (beg in begs2ends) {
end = begs2ends[beg] + 0
beg += 0
if ( ( ($2 >= beg) && ($2 <= end) ) ||
( ($3 >= beg) && ($3 <= end) ) ||
( ($2 <= beg) && ($3 >= end) ) ) {
print $0,$1,beg,end # spacing within $0 unchanged, 3 new fields prefaced with "\t"
next
}
}
# if we get this far it is because we have exhausted the "for" loop
# (ie, found no overlaps) so print current line + "0"
print $0,"0" # spacing within $0 unchanged, 1 new field prefaced with "\t"
}
' file2 file1
This generates:
S 24 96 S 27 93
S 126 352 S 123 348
S 385 465 0
S 548 600 S 542 584
S 621 707 0
S 724 736 S 726 740
With the $1=$1 line uncommented the output becomes:
S 24 96 S 27 93
S 126 352 S 123 348
S 385 465 0
S 548 600 S 542 584
S 621 707 0
S 724 736 S 726 740
S 900 1000 S 901 905
A slight variation on #markp-fuso's answer
Works with GNU awk: saved as overlaps.awk
BEGIN { PROCINFO["sorted_in"] = "#ind_num_asc" }
function in_range(val, min, max) { return min <= val && val <= max }
NR == FNR {
line[FNR] = $0
lo[FNR] = $2
hi[FNR] = $3
next
}
{
overlap = "0"
for (i in line) {
if (in_range(lo[i], $2, $3) || in_range(hi[i], $2, $3)) {
overlap = line[i]
delete line[i]
break
}
}
print $0, overlap
}
Then
gawk -f overlaps.awk file2 file1 | column -t
outputs
S 24 96 S 27 93
S 126 352 S 123 348
S 385 465 0
S 548 600 S 542 584
S 621 707 0
S 724 736 S 726 740
$ cat tst.awk
BEGIN { OFS="\t" }
NR == FNR {
ranges[++numRanges] = $0
next
}
{
overlapped = 0
for ( i=1; i<=numRanges; i++ ) {
range = ranges[i]
split(range,vals)
beg = vals[2]+0
end = vals[3]+0
if ( ( ($2 >= beg) && ($2 <= end) ) ||
( ($3 >= beg) && ($3 <= end) ) ||
( ($2 <= beg) && ($3 >= end) ) ) {
overlapped = 1
break
}
}
if ( overlapped ) {
print $0, range, sprintf("* %d-%d overlaps with %d-%d", beg, end, $2, $3)
}
else {
print $0, 0, sprintf("* nothing in %s overlaps with this range", ARGV[1])
}
}
$ awk -f tst.awk file2 file1 | column -s$'\t' -t
S 24 96 S 27 93 * 27-93 overlaps with 24-96
S 126 352 S 123 348 * 123-348 overlaps with 126-352
S 385 465 0 * nothing in file2 overlaps with this range
S 548 600 S 542 584 * 542-584 overlaps with 548-600
S 621 707 0 * nothing in file2 overlaps with this range
S 724 736 S 726 740 * 726-740 overlaps with 724-736
I have the following file:
chr11_pilon3.g3568.t1 transcript:OIT01734 transcript:OIT01734 1.1e-107 389.8 1000 218 992 1 216 130 345 MDALTRHIQGDVPWCMLFADDIILIDETRAGVSERLEIWRQTLESKGFKISRSKTEYLECKFGDEPSGVGREVMLGSQAIAKRDSVRYLGSVIQGDGEIDGDVTHRIGAGWSKWRLASGVLCDKKIPHKLKGKFFRAMVRPAMFYEAECWPVKNSHIQRMKVAEMRMLRWMCGHTRLDKIKNEVIRQKVGVAPVDKKMGEARLRWFGHVRRRGPDA MDALTRHIQGDVPWCMLFADDIVLIDETRVGVNERLEVWRQTLESKGFKLSRSKTEYLECKFSAESSEVGRDVKLGSQVIAKRDSFRYLGSVIQGEGEIDGDVTHRIGAGWSKWRLASGVLCDKKVPQKLKGKFYRAVVR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LLSVIKQSQKFCPEAIVFIQTLLMAALDRKQRSNSQLDNLMEIKELGPLLCIRSSKVEMDSLDFLTLMDLPEDSQYFHSDNYRTSMLVTVLETLQGFVNVYKELISFPEIFMLISKLLCKMAGENHIPDALREKIKDVSQLIDTKAQEHHMLRQPLKMRKKKPVPIRMLNPKFEENFVKGRDYDPDRERA 389.8 1000 216 85.6 185 31 200 0 0 92.6 0 22IV6AV2SN4IV11IL12GSDA1PS1GE3ED1MK4AV6VF9DE29IV1HQ6FY2MV5FL1EG10IV14CR1HL4KR1KR5QE5PL2KE2GR6FY6GR3 85.6 1.1e-107 99.1
gene.10002.1.1.p1 NisylKD957037g0001.1 NisylKD957037g0001.1 0.0e+00 1218.8 3152 668 780 5 667 122 780 KVIARCRPELAHIPSLEEAPVFHPSEEEFEDTLKYVGSILPHVKHYGICRIVPPSSWKPPSCIEEESTVYGVNTHIQRTSELQNLFFKKRLEGACTRTNNKQQKTLSRKSDFGLDIERKEFGCCNEHFEFENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESGFPHERGVTIHRPQYVESGWNLNNTPKLQDSLLRFGSHESSSILLPRLSIGMCFSSNLWRIEEHHLYLLSYIHFGAPKIFYGVPGSYRCKFEEAVKKHLPQLSAHPCLFQNMAFQFSPSILTSEGIPVYRCVQNPKEFVLILPGAYHAHVDSGFNCSEAVNFAPFDWLPHGQNAVDLYSEQRRKTSISYDKLLFEAATERIRALAELPLLHKKFFDNLKWRAVCRSNEILTKALKSRFATEVRRRKYMCASLESRKMEDDFCATAKRECSICYYDLYLSAIGCTCSPQKYTCLLHAKQLCSCAWREKYLLIRYEIDELNIMVEALDGKVSAVHKWAKEKLGFPVSDFSKDASKDEMKVKSESGQSLDVEQDRKEASIPSVGPSARTNNLNRVTGSWVEADGLSHQPQPKGIVNDTVEVIFPKISQHATVGKNIMISSNTVLKKHLARESSSTKRTVIILSDDEN KVIARCRPELARIPSLEEAPVFHPNTL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G17RG4AP7KN10CY13FVAS6KT1ML16AT4SP13TK3QH12SP3RS36FL4FVSF6EG12VI6-EAV13LV3TS8LS2QR2PS3VI2TKVI2IL15IT19TS9 91.0 0.0e+00 99.3
gene.10002.1.4.p1 NisylKD957037g0001.1 NisylKD957037g0001.1 0.0e+00 1216.8 3147 671 780 9 670 123 780 VIARCRPELAHIPSLEEAPVFHPSEEEFEDTLKYVGSILPHVKHYGICRIVPPSSWKPPSCIEEESTVYGVNTHIQRTSELQNLFFKKRLEGACTRTNNKQQKTLSRKSDFGLDIERKEFGCCNEHFEFENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESGFPHERGVTIHRPQYVESGWNLNNTPKLQDSLLRFGSHESSSILLPRLSIGMCFSSNLWRIEEHHLYLLSYIHFGAPKIFYGVPGSYRCKFEEAVKKHLPQLSAHPCLFQNMAFQFSPSILTSEGIPVYRCVQNPKEFVLILPGAYHAHVDSGFNCSEAVNFAPFDWLPHGQNAVDLYSEQRRKTSISYDKLLFEAATERIRALAELPLLHKKFFDNLKWRAVCRSNEILTKALKSRFATEVRRRKYMCASLESRKMEDDFCATAKRECSICYYDLYLSAIGCTCSPQKYTCLLHAKQLCSCAWREKYLLIRYEIDELNIMVEALDGKVSAVHKWAKEKLGFPVSDFSKDASKDEMKVKSESGQSLDVEQDRKEASIPSVGPSARTNNLNRVTGSWVEADGLSHQPQPKGIVNDTVEVIFPKISQHATVGKNIMISSNTVLKKHLARESSSTKRTVIILSDDEN VIARCRPELARIPSLEEAPVFHPNTLKYVASILPHVKHYGICRIVPPSSWKPPSRIEEPSTVYGVNTHIQRTSDLQNLFFKKRLEGACTRTNNKQQKTLSGKSDFGHDIERKEFGCCNEHFEFENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESSFPHEGDVTSRRPPQYVESGWNLNNTPKLQDSLLRFGSRESSSILLPRLSIGMCFSSNLW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GFESLMSFTLKKVKNSEEACNSVALGSGFAQGPSLVAATSTIISTGKLKSSVRHRPWILDDHVDHIEDDSEFEDDKSLSSSAFLPKGVIRGCSSCHNCQKVIARCRPELARIPSLEEAPVFHPNTLKYVASILPHVKHYGICRIVPPSSWKPPSRIEEPSTVYGVNTHIQRTSDLQNLFFKKRLEGACTRTNNKQQKTLSGKSDFGHDIERKEFGCCNEHFEFENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESSFPHEGDVTSRRPPQYVESGWNLNNTPKLQDSLLRFGSRESSSILLPRLSIGMCFSSNLWRIEEHHLYLLSYIHFGAPKIFYGVPGSYRCKFEEAVKKHLPQLSAHPCLLQNIAFQFSPSVLTSEGIPVYRCVQNPKEFVLLLPGAYHAHADSGFNCSEAVNFAPFDWLPHGQNAVELYSEQGRKTSISYDKLLFEAATEGIRALPELPLLHKNFFDNLKWRAVYRSNEILTKALKSRVSTEVRRRTYLCASLESRKMEDDFCATTKRECPICYYDLYLSAIGCKCSPHKYTCLLHAKQLCPCAWSEKYLLIRYEIDELNIMVEALDGKVSAVHKWAKEKLGLPVSDVFKDASKDGMKVKSESGQSLDIEQDRKEEVSIPSVGPSARTNNVNRVSGSWVEADGSSHRPQSKGIINDKIEVLFPKISQHATVGKNIMTSSNTVLKKHLARESSSTKRSVIILSDDEN 1216.8 3147 664 91.0 604 52 620 3 8 93.4 0 10HR12SNE-E-E-F-E-D-5GA24CR3EP14ED26RG5LH85GS4RGGD2ISHR2-P24HR70FL2MI7IV20IL8VA25DE5RG17RG4AP7KN10CY13FVAS6KT1ML16AT4SP13TK3QH12SP3RS36FL4FVSF6EG12VI6-EAV13LV3TS8LS2QR2PS3VI2TKVI2IL15IT19TS9 91.0 0.0e+00 98.7
gene.10002.1.5.p1 NisylKD957037g0001.1 NisylKD957037g0001.1 0.0e+00 1218.8 3152 668 780 5 667 122 780 KVIARCRPELAHIPSLEEAPVFHPSEEEFEDTLKY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ENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESSFPHEGDVTSRRPPQYVESGWNLNNTPKLQDSLLRFGSRESSSILLPRLSIGMCFSSNLWRIEEHHLYLLSYIHFGAPKIFYGVPGSYRCKFEEAVKKHLPQLSAHPCLLQNIAFQFSPSVLTSEGIPVYRCVQNPKEFVLLLPGAYHAHADSGFNCSEAVNFAPFDWLPHGQNAVELYSEQGRKTSISYDKLLFEAATEGIRALPELPLLHKNFFDNLKWRAVYRSNEILTKALKSRVSTEVRRRTYLCASLESRKMEDDFCATTKRECPICYYDLYLSAIGCKCSPHKYTCLLHAKQLCPCAWSEKYLLIRYEIDELNIMVEALDGKVSAVHKWAKEKLGLPVSDVFKDASKDGMKVKSESGQSLDIEQDRKEEVSIPSVGPSARTNNVNRVSGSWVEADGSSHRPQSKGIINDKIEVLFPKISQHATVGKNIMTSSNTVLKKHLARESSSTKRSVIILSDDEN 1218.8 3152 665 91.0 605 52 621 3 8 93.4 0 11HR12SNE-E-E-F-E-D-5GA24CR3EP14ED26RG5LH85GS4RGGD2ISHR2-P24HR70FL2MI7IV20IL8VA25DE5RG17RG4AP7KN10CY13FVAS6KT1ML16AT4SP13TK3QH12SP3RS36FL4FVSF6EG12VI6-EAV13LV3TS8LS2QR2PS3VI2TKVI2IL15IT19TS9 91.0 0.0e+00 99.3
gene.10002.1.6.p1 NisylKD957037g0001.1 NisylKD957037g0001.1 0.0e+00 1440.2 3727 799 780 15 798 1 780 MGAKRTRSNGESDDGYKLSVPPGFESLMSFTLKKVKNSEEACNSVALESEFAQSPSQVAATSTIISIGKLKSSVRHRPWILDDHVDHIEDDSEFEDDKSLSSIAFLPKGVIRGCSSCHNCQKVIARCRPELAHIPSLEEAPVFHPSEEEFEDTLKYVGSILPHVKHYGICRIVPPSSWKPPSCIEEESTVYGVNTHIQRTSELQNLFFKKRLEGACTRTNNKQQKTLSRKSDFGLDIERKEFGCCNEHFEFENGPKLML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DKSLSSSAFLPKGVIRGCSSCHNCQKVIARCRPELARIPSLEEAPVFHPNTLKYVASILPHVKHYGICRIVPPSSWKPPSRIEEPSTVYGVNTHIQRTSDLQNLFFKKRLEGACTRTNNKQQKTLSGKSDFGHDIERKEFGCCNEHFEFENGPKLMLKYFKHYADHFKKQYFVKEDQITASEPSIQDIEGEYWRIIENPTEEIEVLQGTSAEIKATESSFPHEGDVTSRRPPQYVESGWNLNNTPKLQDSLLRFGSRESSSILLPRLSIGMCFSSNLWRIEEHHLYLLSYIHFGAPKIFYGVPGSYRCKFEEAVKKHLPQLSAHPCLLQNIAFQFSPSVLTSEGIPVYRCVQNPKEFVLLLPGAYHAHADSGFNCSEAVNFAPFDWLPHGQNAVELYSEQGRKTSISYDKLLFEAATEGIRALPELPLLHKNFFDNLKWRAVYRSNEILTKALKSRVSTEVRRRTYLCASLESRKMEDDFCATTKRECPICYYDLYLSAIGCKCSPHKYTCLLHAKQLCPCAWSEKYLLIRYEIDELNIMVEALDGKVSAVHKWAKEKLGLPVSDVFKDASKDGMKVKSESGQSLDIEQDRKEEVSIPSVGPSARTNNVNRVSGSWVEADGSSHRPQSKGIINDKIEVLFPKISQHATVGKNIMTSSNTVLKKHLARESSSTKRSVIILSDDEN 1440.2 3727 786 91.5 719 59 735 3 8 93.5 0 9GS37EG1EG3SG2QL9IT35IS29HR12SNE-E-E-F-E-D-5GA24CR3EP14ED26RG5LH85GS4RGGD2ISHR2-P24HR70FL2MI7IV20IL8VA25DE5RG17RG4AP7KN10CY13FVAS6KT1ML16AT4SP13TK3QH12SP3RS36FL4FVSF6EG12VI6-EAV13LV3TS8LS2QR2PS3VI2TKVI2IL15IT19TS9 91.5 0.0e+00 98.1
The above file has some IDs which are similar
gene.10002.1.1.p1
gene.10002.1.4.p1
gene.10002.1.5.p1
gene.10002.1.6.p1
By remaining only gene.10002 the IDs become identically. I used this awk script (thank you to #anubhava ) to keep only lines of the same ID with smallest value (column 30)
awk '{
if (/^gene\./) {
split($1, a, /\./)
k = a[1] "." a[2]
}
else
k = $1
}
!(k in min) || $30 <= min[k] {
if(!(k in min))
ord[++n] = k
else if (min[k] == $30) {
print
next
}
min[k] = $30
rec[k] = $0
}
END {
for (i=1; i<=n; ++i)
print rec[ord[i]]
}' file
I failed to modified the above awk script to consider the maximum value in column 31 and to keep multiple copies if the column 31 value is the same?
awk '{
if (/^gene\./) {
split($1, a, /\./)
k = a[1] "." a[2]
}
else
k = $1
}
!(k in max) || $31 <= max[k] {
if(!(k in max))
ord[++n] = k
else if (max[k] == $31) {
print
next
}
cov[k] = $31
rec[k] = $0
}
END {
for (i=1; i<=n; ++i)
print rec[ord[i]]
}'
Fixing OP's attempt here, could you please try following. You should change your condition to do comparison for >= condition in $31 >= max[k], since we are looking for maximum value now, added detailed explanation later section of this post too.
awk '{
if (/^gene\./) {
split($1, a, /\./)
k = a[1] "." a[2]
}
else
k = $1
}
!(k in max) || $31 >= max[k] {
if(!(k in max))
ord[++n] = k
else if (max[k] == $31) {
print
next
}
max[k] = $31
rec[k] = $0
}
END {
for (i=1; i<=n; ++i)
print rec[ord[i]]
}' Input_file
Explanation: Adding detailed explanation for above.
awk '{ ##Starting awk program from here.
if (/^gene\./) { ##Checking condition if line is NOT starting from gene. then do following.
split($1, a, /\./) ##Splitting first field into array a with delimiter dot here.
k = a[1] "." a[2] ##Creating variable k with value of a[1] DOT a[2] here.
}
else ##In case line NOT starting from gene. then do following.
k = $1 ##Setting 1st field value to k here.
}
!(k in max) || $31 >= max[k] { ##Checking condition if k is NOT in max array and 31st field is >= max[k]
if(!(k in max)) ##If above any of the condition is true then check if k is NOT present in max
ord[++n] = k ##Creating ord with index of increasing value of n and its value is k
else if (max[k] == $31) { ##else printing maximum duplicate line, no need to keep appending it in array.
print ##Printing it here.
next ##next will skip all further statements from here.
}
max[k] = $31 ##Creating max with index of k and value of 31st field.
rec[k] = $0 ##Creating rec with index of k and value of current line.
}
END { ##Starting END block of this program from here.
for (i=1; i<=n; ++i) ##Starting a for loop from i=1 to till value of n here.
print rec[ord[i]] ##Printing array rec with index of; value of ord array which has i index.
}' Input_file ##Mentioning Input_file name here.
I have data which looks like this
1 3
1 2
1 9
5 4
4 6
5 6
5 8
5 9
4 2
I would like the output to be
1 3,2,9
5 4,6,8,9
4 6,2
This is just sample data but my original one has lots more values.
So this worked
So this basically creates a hash table, using the first column as a key and the second column of the line as the value:
awk '{line="";for (i = 2; i <= NF; i++) line = line $i ", "; table[$1]=table[$1] line;} END {for (key in table) print key " => " table[key];}' trial.txt
OUTPUT
4 => 6, 2
5 => 4, 6, 8, 9
1 => 3, 2, 9
I'd write
awk -v OFS=, '
{
key = $1
$1 = ""
values[key] = values[key] $0
}
END {
for (key in values) {
sub(/^,/, "", values[key])
print key " " values[key]
}
}
' file
If you want only the unique values for each key (requires GNU awk for multi-dimensional arrays)
gawk -v OFS=, '
{ for (i=2; i<=NF; i++) values[$1][$i] = i }
END {
for (key in values) {
printf "%s ", key
sep = ""
for (val in values[key]) {
printf "%s%s", sep, val
sep = ","
}
print ""
}
}
' file
or perl
perl -lane '
$key = shift #F;
$values{$key}{$_} = 1 for #F;
} END {
$, = " ";
print $_, join(",", keys %{$values{$_}}) for keys %values;
' file
if not concerned with the order of the keys, I think this is the idiomatic awk solution.
$ awk '{a[$1]=($1 in a?a[$1]",":"") $2}
END{for(k in a) print k,a[k]}' file |
column -t
4 6,2
5 4,6,8,9
1 3,2,9
I have the following two files:
File 1:
1 290 rs1345
2 450 rs5313
1 1120 rs4523
2 790 rs4325
File 2:
1 201 LDLR
2 714 APOA5
1 818 NOTCH5
1 514 TTN
I wish to isolate only the rows in file 2 in which the second field is within 100 units of the second field in file 1 (if field 1 matches):
Desired output: (note the third field is from the matching line in file1).
1 201 LDLR rs1345
2 714 APOA5 rs4325
I tried using the following code:
for i in {1..4} #there are 4 lines in file2
do
chr=$(awk 'NR=="'${i}'" { print $1 }' file2)
pos=$(awk 'NR=="'${i}'" { print $2 }' file2)
gene=$(awk 'NR=="'${i}'" { print $3 }' file2)
start=$(echo $pos | awk '{print $1-100}') #start and end variables for 100 unit range
end=$(echo $pos | awk '{print $1+100}')
awk '{if ($1=="'$chr'" && $2 > "'$start'" && $2 < "'$end'") print "'$chr'","'$pos'","'$gene'"$3}' file1
done
The code is not working, I believe something is wrong with my start and end variables, because when I echo $start, I get 414, which doesn't make sense to me and I get 614 when i echo $end.
I understand this question might be difficult to understand so please ask me if any clarification is necessary.
Thank you.
The difficulty is that $1 is not a unique key, so some care needs to be taken with the data structure to store the data in file 1.
With GNU awk, you can use arrays of arrays:
gawk '
NR==FNR {f1[$1][$2] = $3; next}
$1 in f1 {
for (val in f1[$1])
if (val-100 <= $2 && $2 <= val+100)
print $0, f1[$1][val]
}
' file1 file2
Otherwise, you have to use a one-dimensional array and stuff 2 pieces of information into the key:
awk '
NR==FNR {f1[$1,$2] = $3; next}
{
for (key in f1) {
split(key, a, SUBSEP)
if (a[1] == $1 && a[2]-100 <= $2 && $2 <= a[2]+100)
print $0, f1[key]
}
}
' file1 file2
That works with mawk and nawk (and gawk)
#!/usr/bin/python
import pandas as pd
from StringIO import StringIO
file1 = """
1 290 rs1345
2 450 rs5313
1 1120 rs4523
2 790 rs4325
"""
file2 = """
1 201 LDLR
2 714 APOA5
1 818 NOTCH5
1 514 TTN
"""
sio = StringIO(file1)
df1 = pd.read_table(sio, sep=" ", header=None)
df1.columns = ["a", "b", "c"]
sio = StringIO(file2)
df2 = pd.read_table(sio, sep=" ", header=None)
df2.columns = ["a", "b", "c"]
df = pd.merge(df2, df1, left_on="a", right_on="a", how="outer")
#query is intuitive
r = df.query("b_y-100 < b_x <b_y + 100")
print r[["a", "b_x", "c_x", "c_y"]]
output:
a b_x c_x c_y
0 1 201 LDLR rs1345
7 2 714 APOA5 rs4325
pandas is the right tool to do such tabular data manipulation.
I'm trying to take a 3 column input file and separate it based on a condition in column 3. I think it'll be easier to show you than explain:
Input File:
outputfile1.txt
26 NCC 1 # First Start
38 NME 2
44 NSC 1 # Start2
56 NME 2
62 NCC 1 # Start3
...
314 NCC 1 # Start17
326 NME 2
332 NSC 1 # Start18
344 NME 2
349 NME 2 # Final End
(The hashed comments aren't part of the file, I've added to make things clearer).
Column 3 is used to determine a new "START" entry
"START/END" values are from Column 1
"TITLE" I would like to be all values from Column 2 between consecutive "STARTS"
Desired Output
outputfile2.txt
START=26 ; END=43 ; TITLE=NCC_NME
START=44 ; END=61 ; TITLE=NSC_NME
START=62 ; END=79 ; TITLE=NCC_...
...
START=314 ; END=331 ; TITLE=NCC_NME
START=332 ; END=349 ; TITLE=NSC_NME
Crude script that 'almost' does this but makes 5 single column temporary files in the process.
awk '{ print $1 }' outputfile1.txt | sed '$d' > tempfile1.txt
awk '{ print $1-1 }' outputfile1.txt | sed '$d' > tempfile2.txt
sed '$d' outputfile1.txt | awk 'NR{print $3-p}{p=$3}' > tempfile3.txt
awk ' { getline value < "tempfile1.txt" }
{ if (NR==1)
print value ;
else if( $1 != 1 )
print value }' tempfile3.txt > tempfile4.txt
awk ' { getline value < "tempfile2.txt" }
{ if (NR==1)
print value ;
else if ( $1 != 1 )
print value }' tempfile3.txt | sed '1d' > tempfile5.txt
awk 'END{print $1}' outputfile1.txt >> tempfile5.txt
awk ' { getline value < "tempfile5.txt" }
{print "START="$0 " ; END="value}' tempfile4.txt > outputfile2.txt
Contents of temp files
| temp1 temp2 temp3
NR=1 | 26 25 1
NR=2 | 38 37 1
NR=3 | 44 43 -1
NR=4 | 56 55 1
NR=5 | 62 61 -1
... | ... ... ...
NR=33 | 314 313 -1
NR=34 | 326 325 1
NR=35 | 332 331 -1
NR=36 | 344 343 1
----------------------------------
| temp4 temp5
NR=1 | 26 43
NR=2 | 44 61
NR=3 | 62 79
... | ... ...
NR=17 | 314 331
NR=18 | 332 359
Current output
outputfile2.txt
START=26 ; END=43
START=44 ; END=61
START=62 ; END=79
...
START=314 ; END=331
START=332 ; END=349
Try:
awk '
function print_range() {
printf "START=%s ; END=%s ; TITLE=%s\n", start, end-1, title
}
{
end=$1
}
# if column 3 is equal to 1, then there is a new start
$3==1 {
if(title) print_range()
start=$1
title=$2
next
}
# if the label in field 2 is not part of the title then add it
title!~"(^|_)" $2 "(_|$)" {
title=title"_"$2
}
END {
end++
print_range()
}
' file
You can do everything in one go using:
awk '{
if(NR==1){
# if we are the first record we initialize our variables
PREVIOUS_ONE=$1
TITLE=$2
PREVIOUS_THIRD=$3
} else {
# as long as the new third column is larger we update our variables
if(PREVIOUS_THIRD < $3) {
TITLE=TITLE"_"$2
PREVIOUS_THIRD=$3
} else {
# this means the third column was smaller
# we print out the data and reinitialize our variables
print "START="PREVIOUS_ONE" ; END="$1-1" ; TITLE= "TITLE;
PREVIOUS_ONE=$1
TITLE=$2
PREVIOUS_THIRD=$3
}
}
}' outputfile1.txt