That is pretty easy to do with sed and you not even need to use the hold space (the sed auxiliary buffer). Given the input
file below:
$ cat input
@"Afghanistan.png",
@"Albania.png",
@"Algeria.png",
@"American_Samoa.png",
you should use this command:
sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input
The result:
$ sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input
@"Afghanistan.png",
@"Afghanistan",
@"Albania.png",
@"Albania",
@"Algeria.png",
@"Algeria",
@"American_Samoa.png",
@"American_Samoa",
This commands is just a replacement command (s///
). It matches anything starting with @"
followed by non-period chars ([^.]*
) and then by .png",
. Also, it matches all non-period chars before .png",
using the group brackets \(
and \)
, so we can get what was matched by this group. So, this is the to-be-replaced regular expression:
@"\([^.]*\)\.png",
So follows the replacement part of the command. The &
command just inserts everything that was matched by @"\([^.]*\)\.png",
in the changed content. If it was the only element of the replacement part, nothing would be changed in the output. However, following the &
there is a newline character - represented by the backslash \
followed by an actual newline - and in the new line we add the @"
string followed by the content of the first group (\1
) and then the string ",
.
This is just a brief explanation of the command. Hope this helps. Also, note that you can use the \n
string to represent newlines in some versions of sed (such as GNU sed). It would render a more concise and readable command:
sed 's/@"\([^.]*\)\.png",/&\n@"\1",/' input