$ cat file
>hCoV-19/xxx/xxx-YYY/xxx
>hCoV-19/xxx/xxx-ZZZ/xxx
$ awk -F '[/-]' '{ printf "%s %s\n", $5, $0 }' file
YYY >hCoV-19/xxx/xxx-YYY/xxx
ZZZ >hCoV-19/xxx/xxx-ZZZ/xxx
The awk
code above treats the data as lines that are divided into fields on either /
or -
. The fifth such field is the field that you want to prepend to each line, which is what the printf
statement does.
If the -
is not a good delimiter (it wouldn't be if the string before the first slash sometimes didn't contain a dash, for example), then use only /
as a delimiter, split the third slash-delimited field on -
, and prepend the second bit of the result to the line:
$ awk -F / '{ split($3,a,"-"); printf "%s %s\n", a[2], $0 }' file
YYY >hCoV-19/xxx/xxx-YYY/xxx
ZZZ >hCoV-19/xxx/xxx-ZZZ/xxx
Using sed
:
$ sed 's/.*-\([^/]*\).*/\1 &/' file
YYY >hCoV-19/xxx/xxx-YYY/xxx
ZZZ >hCoV-19/xxx/xxx-ZZZ/xxx
or, if you're on Plan9 or using the Plan9 sed
implementation which has issues with the /
inside the bracketed expression, use an alternative set of delimiters for the s///
command:
$ sed 's,.*-\([^/]*\).*,\1 &,' file
YYY >hCoV-19/xxx/xxx-YYY/xxx
ZZZ >hCoV-19/xxx/xxx-ZZZ/xxx
The regular expression used here captures the substring consisting of no /
characters after the last -
on the line. It then prepends the line with this captured substring and a space.
Note that the main difference between this sed
solution and the awk
solution further up is that the awk
code uses the field-like structure of each line, while the sed
code is more "sloppy", just looking for a string of non-slash characters after a dash.
The https://regexr.com/
site currently supports JavaScript regular expressions and Perl-compatible regular expressions (PCRE). You are not using any of those two languages here, so whatever the site is telling you may not work. awk
is using POSIX extended regular expressions (EREs), and most other standard Unix tools for text manipulation, including sed
, uses POSIX basic regular expressions (BREs).
See also Why does my regular expression work in X but not in Y?