So, I am having the following line in a text file:

CN=blabla:client:blablafile.jks, 26-Oct-2019, fabric, hostname:/path/to/jks/file/

I need to move the :blablafile.jks to the end of the line and the output to be something like:

CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/, blablafile.jks

So, I need to remove the : character as well when moving the blablafile.jks to the end of the line.

Tried some things with the sed command, but I only sorted out how to move the date to the end of the file. Not sure how should I proceed to have the desired output though.

Can someone help me a bit?


Also, can that specific command be built in a way to have all of the *.jks files moved to the end of each line?


CN=blabla:client:blablafile.jks, 26-Oct-2019, server, hostname:/path/to/jks/file/
CN=blabla:client:blablafile31242.jks, 26-Oct-2019, server, hostname:/path/to/jks/file/
CN=blabla:client:blablafile3222242.jks, 26-Oct-2019, server, hostname:/path/to/jks/file/

to become:

CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/, blablafile.jks
CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/, blablafile31242.jks
CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/, blablafile3222242.jks

2 Answers 2


Using sed:

sed -E 's/(.*):([^,]*\.jks)(,.*)/\1\3, \2/' infile

Anything inside (...) is known a captured group and they will take a back-reference like \1 for first group, \2 for second,... (up to maximum \9 only).

: matches literal :
[^,]* matches any character except comma;
\.jks matches literal dot . followed by jks

:[^,]*\.jks, matches a colon followed by anything that ends with .jks and a comma; we want this part ([^,]*\.jks) to move to last, so we surrounded it within (...); the colon : we will remove it, so we left that outside the captured groups.

Outer captured groups captures anything (.*) around above captured group; first one is accessible with \1 "(.*)", second with \2 "([^,]*\.jks)" and third \3 "(,.*)".


Tested in GNU awk.

  • split() first field into array a and then reassign $1 to a[1] and a[2] values and create $5 with a[3] value.
  • OFS as separator.
awk -F, '{split($1,a,":",sep);$1=a[1]sep[1]a[2];  $5=a[3]; print $0}' OFS=',' file
CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/,blablafile.jks
CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/,blablafile31242.jks
CN=blabla:client, 26-Oct-2019, server, hostname:/path/to/jks/file/,blablafile3222242.jks

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .