Assume a multi-line text file with two alternating types of lines. The first line starts with ">" and contains alphanumerical strings separated by underscores. The second line consists of a single alphanumeric string.
$ cat file
>foo_bar_baz1
abcdefghijklmnopqrstuvwxyz0123456789
>foo_bar_baz2
abcdefghijklmnopqrstuvwxyz0123456789
>foo_bar_baz3
abcdefghijklmnopqrstuvwxyz0123456789
I would like to change the order of the words in those lines starting with ">".
$ cat file | sought_command
>baz1_foo_bar
abcdefghijklmnopqrstuvwxyz0123456789
>baz2_foo_bar
abcdefghijklmnopqrstuvwxyz0123456789
>baz3_foo_bar
abcdefghijklmnopqrstuvwxyz0123456789
I understand that this task can be done with awk.
How would I need to change the below draft awk code to achieve my objective? In its current form, the below code only prints lines starting with ">", but not those without.
awk -F'_' '$1 ~ /^>/ { print ">"$3"_"$1"_"$2}' file | sed 's/>foo/foo/'
>baz1_foo_bar
>baz2_foo_bar
>baz3_foo_bar