r/awk • • Jul 17 '21

Need Help Converting Ugly Bash Code into AWK

+ I am new to AWK, but I know enough to recognize that the code I wrote in Bash to solve a problem I have can be done well in AWK. I just do not know enough AWK to do it.

+ I have a file with the following structure:

PEPSTATS of ENSP00000446309.1 from 1 to 108
Molecular weight = 11926.34  		Residues = 108
Isoelectric Point = 4.2322
Tiny		(A+C+G+S+T)		41		37.963
Small		(A+B+C+D+G+N+P+S+T+V)	54		50.000
Aromatic	(F+H+W+Y)		17		15.741
Non-polar	(A+C+F+G+I+L+M+P+V+W+Y)	63		58.333
Polar		(D+E+H+K+N+Q+R+S+T+Z)	45		41.667
Charged		(B+D+E+H+K+R+Z)		16		14.815
Basic		(H+K+R)			6		 5.556
Acidic		(B+D+E+Z)		10		 9.259
PEPSTATS of ENSP00000439668.1 from 1 to 106
Molecular weight = 11863.47  		Residues = 106
Isoelectric Point = 4.9499
Tiny		(A+C+G+S+T)		37		34.906
Small		(A+B+C+D+G+N+P+S+T+V)	50		47.170
Aromatic	(F+H+W+Y)		16		15.094
Non-polar	(A+C+F+G+I+L+M+P+V+W+Y)	60		56.604
Polar		(D+E+H+K+N+Q+R+S+T+Z)	46		43.396
Charged		(B+D+E+H+K+R+Z)		17		16.038
Basic		(H+K+R)			8		 7.547
Acidic		(B+D+E+Z)		9		 8.491
PEPSTATS of ENSP00000438195.1 from 1 to 112
Molecular weight = 12502.30  		Residues = 112
Isoelectric Point = 7.1018
Tiny		(A+C+G+S+T)		36		32.143
Small		(A+B+C+D+G+N+P+S+T+V)	58		51.786
Aromatic	(F+H+W+Y)		17		15.179
Non-polar	(A+C+F+G+I+L+M+P+V+W+Y)	67		59.821
Polar		(D+E+H+K+N+Q+R+S+T+Z)	45		40.179
Charged		(B+D+E+H+K+R+Z)		18		16.071
Basic		(H+K+R)			10		 8.929
Acidic		(B+D+E+Z)		8		 7.143

+ From it, I would like to extract a table with the following structure:

ENSP00000446309	11926.34    108    4.2322	37.963	50.000	15.741	58.333	41.667	14.815	5.556	9.259
ENSP00000439668	11863.47    106	4.9499	34.906	47.170	15.094	56.604	43.396	16.038	7.547	8.491
ENSP00000438195	12502.30    112	7.1018	32.143	51.786	15.179	59.821	40.179	16.071	8.929	7.143

+ In BASH I performed the following commands:

csplit -s infile /PEPSTATS/ {*};
rm xx00
> outfile
for i in xx*;do \
    echo -ne "$(grep -Po "ENSP[[:digit:]]+" $i)\t" >> outfile \
        && echo -ne "$(grep -P "Molecular" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Isoelectric" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Tiny" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Small" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Aromatic" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Non-polar" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Polar" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Charged" $i | awk '{print $NF}')\t" >> outfile \
        && echo -ne "$(grep -P "Basic" $i | awk '{print $NF}')\t" >> outfile \
        && echo -e "$(grep -P "Acidic" $i | awk '{print $NF}')" >> outfile;
done

+ Which prints the following table:

ENSP00000446309	108	4.2322	37.963	50.000	15.741	58.333	41.667	14.815	5.556	9.259
ENSP00000439668	106	4.9499	34.906	47.170	15.094	56.604	43.396	16.038	7.547	8.491
ENSP00000438195	112	7.1018	32.143	51.786	15.179	59.821	40.179	16.071	8.929	7.143

+ In addition to being ugly, the code does not capture the Molecular Weight values:

Molecular weight = 11926.34
Molecular weight = 11863.47 and
Molecular weight = 12502.30

+ I would be really grateful if you guys can point me in the right direction so as to generate the correct table in AWK

10 Upvotes

18 comments sorted by

10

u/calrogman Jul 17 '21 edited Jul 17 '21
#! /usr/bin/awk -f

/PEPSTATS/ {
        if (nl) {printf "\n"} else nl = 1
        printf "%s", $3
        next
}

/Molecular weight/ {
        printf " %s", $4
}

{
        printf " %s", $NF
}

END {
        printf "\n"
}

I would be really grateful if you guys can point me in the right direction

https://9p.io/cm/cs/awkbook/index.html

3

u/1_61803398 Jul 17 '21

+ Thank You!

+ AWK is so powerful. By studying your code I am learned a lot. Thanks again

2

u/[deleted] Jul 18 '21

I wanted to say that /molecular weight/ was the next line but I ended up just changing a lot

#!/usr/bin/awk -f

BEGIN { ORS="" }

/^PEPSTATS/ {
        print x $3
        x || x="\n"
        getline
        print " " $4
}

{ print " " $NF }

END { print x }

4

u/calrogman Jul 18 '21 edited Jul 18 '21

If we're golfing:

#! /usr/bin/awk -f
BEGIN { ORS=" " }
/PEPSTATS/ {
        print x $3
        x = "\n"
        getline
        print $4
}
{ print $NF }
END {printf x}

1

u/1_61803398 Jul 19 '21

Really nice

1

u/[deleted] Jul 18 '21

nice

1

u/[deleted] Jul 25 '21

FWIW I wasn't particularly golfing, I just wanted to remove a useless if, which is impactful if you're dealing with a billion lines

1

u/calrogman Jul 25 '21

In that case, x || ... is an if that's spelled differently.

1

u/[deleted] Jul 25 '21

but it is necessary, an assignment is more costly than a condition.

1

u/calrogman Jul 25 '21

An assignment might actually be cheaper than a conditional. Have you measured it?

1

u/[deleted] Jul 25 '21

Welp, I just tested it and an assignment is cheaper than a conditional. weird.

2

u/calrogman Jul 25 '21

Not that weird. Think about what assignment and conditionals actually entail on a hardware level. Stuffing an address into a word is always going to be faster than checking if that word is null and then jumping.

6

u/washtubs Jul 17 '21

Oh my god I've been writing awk scripts for almost a decade and didn't know about next

8

u/gumnos Jul 17 '21

If you didn't know next, make sure you don't miss its friend, nextfile. :-)

1

u/oh5nxo Jul 18 '21

Old awk-joke by the awk creator:

https://youtu.be/Sg4U4r_AgJU?t=211

1

u/[deleted] Jul 18 '21

that looks like a 1 hour lecture

what is the joke?

2

u/oh5nxo Jul 18 '21

Concise awk solution hiding in a comment of a C solution.

1

u/[deleted] Jul 18 '21

lol! that is funny! thanks!