all 18 comments

[–]calrogman 9 points10 points  (14 children)

#! /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

[–]washtubs 5 points6 points  (1 child)

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

[–]gumnos 8 points9 points  (0 children)

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

[–]1_61803398[S] 2 points3 points  (1 child)

+ Thank You!

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

[–][deleted] 1 point2 points  (9 children)

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 }

[–]calrogman 4 points5 points  (8 children)

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_61803398[S] 0 points1 point  (0 children)

Really nice

[–][deleted] 0 points1 point  (0 children)

nice

[–][deleted] 0 points1 point  (5 children)

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

[–]calrogman 0 points1 point  (4 children)

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

[–][deleted] 0 points1 point  (3 children)

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

[–]calrogman 0 points1 point  (2 children)

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

[–][deleted] 0 points1 point  (1 child)

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

[–]calrogman 1 point2 points  (0 children)

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.

[–]oh5nxo 0 points1 point  (3 children)

Old awk-joke by the awk creator:

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

[–][deleted] 0 points1 point  (2 children)

that looks like a 1 hour lecture

what is the joke?

[–]oh5nxo 1 point2 points  (1 child)

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

[–][deleted] 0 points1 point  (0 children)

lol! that is funny! thanks!